【问题标题】:How to get the request.body value in asp classic?如何在 asp classic 中获取 request.body 值?
【发布时间】:2019-05-28 13:17:44
【问题描述】:

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

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

【问题讨论】:

    标签: asp-classic


    【解决方案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
    

    【讨论】:

    • 非常感谢 Adam :-) 现在正在获取 JSON。但它说我不能在 BinaryRead 之后使用 Request.Form。这是一个 webhook url,我想发送一些我收到的变量以及 JSON 数据,这可能吗?
    • 所以他们将 JSON 发送到我的 web hook url,我想用那个 url 来做这个,mydomain.se/getorderinfo.asp?personal_id=1234ordernummer=789。那么,如果在 BinaryRead 之后不能使用 request.form,我该如何获取这些值?
    • 我不太确定你想要做什么,但Request.Form 用于检索表单数据,而不是设置它。如果您想回发给 webhook 发件人,您需要使用诸如 MSXML2.ServerXMLHTTP 之类的方法来创建并发送 POST 正文。您可以在帖子正文中包含变量,这是一个很好的例子:*.com/a/5302015/4901783
    • 等等,您是说 JSON 被发布到:mydomain.se/getorderinfo.asp?personal_id=1234&amp;ordernummer=789,并且您想要检索 personal_idordernumber 值以及 JSON?如果是这样,您需要使用request.querystring 而不是request.form
    • 是的,JSON 已发布到我的 webhook mydomain.se/getorderinfo.asp?personal_id=1234&ordernummer=789 的 url 中,好的,我会测试它。非常感谢。
    最近更新 更多