首页 > 解决方案 > 如何在 asp classic 中获取 request.body 值?

问题描述

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

那么我如何从 a 中获取值request.body

标签: asp-classic

解决方案


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

要阅读它,您需要使用Request.BinaryReadwith 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

推荐阅读