【发布时间】:2019-05-28 13:17:44
【问题描述】:
在一个 .asp 经典页面中,我收到了一个 POST 发送给我(一个 JSON 字符串),它是在 request.body 中发送的,这个人说如何发送它。
但如果我只有theresponse=request.form 我什么都得不到?
那么我如何从request.body 获取值?
【问题讨论】:
标签: asp-classic
在一个 .asp 经典页面中,我收到了一个 POST 发送给我(一个 JSON 字符串),它是在 request.body 中发送的,这个人说如何发送它。
但如果我只有theresponse=request.form 我什么都得不到?
那么我如何从request.body 获取值?
【问题讨论】:
标签: asp-classic
我过去使用的一些支付网关 API 以这种方式发送响应。数据 (JSON) 作为二进制正文发送。
要阅读它,您需要使用Request.BinaryRead 和Request.TotalBytes,然后使用Adodb.Stream 将二进制转换为UTF8 文本:
Response.ContentType = "application/json"
Function BytesToStr(bytes)
Dim Stream
Set Stream = Server.CreateObject("Adodb.Stream")
Stream.Type = 1 'adTypeBinary
Stream.Open
Stream.Write bytes
Stream.Position = 0
Stream.Type = 2 'adTypeText
Stream.Charset = "utf-8"
BytesToStr = Stream.ReadText
Stream.Close
Set Stream = Nothing
End Function
' You shouldn't really be receiving any posts more than a few KB,
' but it might be wise to include a limit (200KB in this example),
' Anything larger than that is a bit suspicious. If you're dealing
' with a payment gateway the usual protocol is to post the JSON
' back to them for verification before processing.
if Request.TotalBytes > 0 AND Request.TotalBytes <= 200000 then
Dim postBody
postBody = BytesToStr(Request.BinaryRead(Request.TotalBytes))
Response.Write(postBody) ' the JSON... hopefully
end if
【讨论】:
Request.Form 用于检索表单数据,而不是设置它。如果您想回发给 webhook 发件人,您需要使用诸如 MSXML2.ServerXMLHTTP 之类的方法来创建并发送 POST 正文。您可以在帖子正文中包含变量,这是一个很好的例子:*.com/a/5302015/4901783
mydomain.se/getorderinfo.asp?personal_id=1234&ordernummer=789,并且您想要检索 personal_id 和 ordernumber 值以及 JSON?如果是这样,您需要使用request.querystring 而不是request.form。