如何获得ASP Classic中的request.body值?

时间:2019-05-28 13:17:45

标签: asp-classic

在一个.asp经典页面中,我得到了一个POST发送给我(一个JSON字符串),并以request.body发送,这家伙说如何发送。 但是,如果我只有theresponse=request.form,我什么也没得到?

那我该如何从request.body中获取值?

1 个答案:

答案 0 :(得分:1)

我过去使用过的某些支付网关API已以这种方式发送了响应。数据(JSON)作为二进制正文发送。

要阅读它,您需要将Request.BinaryReadRequest.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