|
导读:
一般情况下,ASP都是用来Request.Form来获取post/get提交过来的数据,但实际上呢,如果是java或php提交过来的话,用Request.Form得到的可能就是空值。因此,最稳妥的办法是使用二进制流得到数据。
第一种方法:根据键名获取数据
- <%
- '1.得到Request表单数据字节数
- FormSize = Request.TotalBytes
- '2.使用二进制读取Request表单数据
- FormData = Request.BinaryRead(FormSize)
- '3.创建对象数组
- Set dataArray = Server.CreateObject("Scripting.Dictionary") ' Server.CreateObject("Scripting.Dictionary")
- dataArray.Add "pay_id", GetFormVal(FormData,"pay_id")
- dataArray.Add "pay_money", GetFormVal(FormData,"pay_money")
- dataArray.Add "pay_price", GetFormVal(FormData,"pay_price")
- dataArray.Add "pay_no", GetFormVal(FormData,"pay_no")
- dataArray.Add "pay_time", GetFormVal(FormData,"pay_time")
- dataArray.Add "pay_type", GetFormVal(FormData,"pay_type")
- dataArray.Add "param", GetFormVal(FormData,"param")
- dataArray.Add "sign", GetFormVal(FormData,"sign")
- '4.输出测试
- response.write(dataArray("sign"))
-
- '/**获取表单数据里的值
- ' * @param FormData 二进制的表单数据
- ' * @param FormName 键名
- ' */
- Function GetFormVal(FormData,FormName)
- 'Chr返回一个Unicode字符,2个字节
- 'ChrB返回一个ANSI字符,1个字节
- charBs = ChrB(13) & ChrB(10)
- Divider = LeftB(FormData, InStrB(FormData, charBs) - 1)
- GetFormVal = ""
- StartPos = LenB(Divider) + 2
- FormName = Chr(34) & FormName & Chr(34)
- Do While StartPos > 0
- strlen = InStrB(StartPos, FormData, charBs) - StartPos
- SearchStr = MidB(FormData, StartPos, strlen)
- If InStr(binTostr(SearchStr,"utf-8"), FormName) > 0 Then
- ValStart = InStrB(StartPos, FormData, charBs & charBs) + 4
- ValLen = InStrB(StartPos, FormData, Divider) - 2 - ValStart
- ValContent = MidB(FormData, ValStart, ValLen)
- If GetFormVal <> "" Then
- GetFormVal = GetFormVal & "," & binTostr(ValContent,"utf-8")
- Else
- '二进制转化
- GetFormVal = binTostr(ValContent,"utf-8")
- End If
- End If
- If InStrB(StartPos, FormData, Divider) < 1 Then
- Exit Do
- End If
- StartPos = InStrB(StartPos, FormData, Divider) + LenB(Divider) + 2
- Loop
- End Function
-
- '/**二进制转化(将二进制转换为字符串)
- ' * @param vin 要转换的二进制
- ' * @param charset 编码方式 utf-8或者gb2312
- ' */
- Function binTostr(vin,charset)
- Const adTypeText = 2
- Dim BytesStream,StringReturn
- Set BytesStream = Server.CreateObject("ADODB.Stream")
- With BytesStream
- .Type = adTypeText
- .Open
- .WriteText vin
- .Position = 0
- .Charset = charset
- .Position = 2
- StringReturn = .ReadText
- .Close
- End With
- Set BytesStream = Nothing
- binTostr = StringReturn
- End Function
- %>
复制代码
第二种方法:直接返回完整的键值对数组
- <%
- '1.得到Request表单数据字节数
- FormSize = Request.TotalBytes
- '2.使用二进制读取Request表单数据
- FormData = Request.BinaryRead(FormSize)
- '3.获取键值对格式的数组
- Set dataArray=GetFormData(FormData)
- '4.输出测试
- response.write(dataArray("sign"))
-
- '/**获取表单数据
- ' * @param FormData 二进制的表单数据
- ' * @return 返回键值对格式的数组
- ' */
- Function GetFormData(FormData)
- 'Chr返回一个Unicode字符,2个字节
- 'ChrB返回一个ANSI字符,1个字节
- charBs = ChrB(13) & ChrB(10)
- Divider = LeftB(FormData, InStrB(FormData, charBs) - 1)
- StartPos = LenB(Divider) + 2
- '创建对象数组
- Set dataArray = Server.CreateObject("Scripting.Dictionary") ' Server.CreateObject("Scripting.Dictionary")
- Do While StartPos > 0
- strlen = InStrB(StartPos, FormData, charBs) - StartPos
- SearchStr = MidB(FormData, StartPos, strlen)
- '1.获取单个表单元素属性,如:Content-Disposition: form-data; name="键名"
- FormItem=binTostr(SearchStr,"utf-8")
- '2.获取键名
- FormName=Right(FormItem,Len(FormItem)-Instr(FormItem,"="))
- '3.去掉键名的双引号
- FormName = Replace(FormName, """", "")
- FormValue=""
- If FormName <> "" Then
- ValStart = InStrB(StartPos, FormData, charBs & charBs) + 4
- ValLen = InStrB(StartPos, FormData, Divider) - 2 - ValStart
- ValContent = MidB(FormData, ValStart, ValLen)
- If FormValue <> "" Then
- FormValue = FormValue & "," & binTostr(ValContent,"utf-8")
- Else
- '二进制转化
- FormValue = binTostr(ValContent,"utf-8")
- End If
- '添加元素到数组中
- dataArray.Add FormName, FormValue
- End If
- If InStrB(StartPos, FormData, Divider) < 1 Then
- Exit Do
- End If
- StartPos = InStrB(StartPos, FormData, Divider) + LenB(Divider) + 2
- Loop
- Set GetFormData=dataArray
- Set dataArray=nothing
- End Function
-
- '/**二进制转化(将二进制转换为字符串)
- ' * @param vin 要转换的二进制
- ' * @param charset 编码方式 utf-8或者gb2312
- ' */
- Function binTostr(vin,charset)
- Const adTypeText = 2
- Dim BytesStream,StringReturn
- Set BytesStream = Server.CreateObject("ADODB.Stream")
- With BytesStream
- .Type = adTypeText
- .Open
- .WriteText vin
- .Position = 0
- .Charset = charset
- .Position = 2
- StringReturn = .ReadText
- .Close
- End With
- Set BytesStream = Nothing
- binTostr = StringReturn
- End Function
- %>
复制代码 |
|