vbscript - 使用经典 ASP VBScript 时无法获取原始 POST 数据

标签 vbscript asp-classic http-post

我已经尝试了两天来设置满足第 3 方提供商要求的端点。他们将通过 HTTPS POST 向我们发送有关业务对象状态的更新,请求的内容将是 JSON。不幸的是,它现在必须用 VBScript 编写。

目前,我无法获得他们发送给我的请求的原始内容,所以我根本无法处理它。

我创建了一个简单的端点 (raw-form.asp) 和两个测试页面来演示该问题。首先,我使用 HTML 表单设置了一个简单的测试 HTML 页面 (raw-form-test1.asp),它工作正常。第二个测试页 (raw-form-test2.asp) 使用 WinHttpRequest 将内容发送到端点。使用它时,数据不存在。我正在尝试通过 Request.Body 获取它。

原始形式的 asp:

<%
    Dim post : post = Request.Body
    Response.ContentType = "text/plain"
    Response.Write "Your " & Request.ServerVariables("REQUEST_METHOD") & " data was: " & post
%>

原始表格测试1.asp:

<!DOCTYPE html>
<html>
    <body>
        <form action="raw-form.asp" method="post">
            <p><textarea name="data"></textarea></p>
            <p><input type="submit"></p>
        </form>
    </body>
</html>

原始表格测试2.asp:

<%
    Dim data : data = Request.Form("data")
    Dim resp : resp = ""

    If data <> "" Then
        Dim http : Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
        http.Open "post", "http://localhost:8080/raw-form.asp"
        http.Send data
        http.WaitForResponse(10)
        resp = http.Status & " | " & http.ResponseText
    End If
%>
<!DOCTYPE html>
<html>
    <body>
        <%= Server.HTMLEncode(resp) %>
        <form action="raw-form-test2.asp" method="post">
            <p><textarea name="data"></textarea></p>
            <p><input type="submit"></p>
        </form>
    </body>
</html>

当填写一些随机文本并提交第一个测试时,响应正文如我所料:

您的 POST 数据是:data=abc

使用第二个测试时,resp中的返回结果为:

200 |您的 POST 数据是:

我也尝试过使用 Request.BinaryRead() 但没有成功(VBScript 得到它的长度,但不是内容 - 可能只是 VB 对类型很糟糕)。我希望有另一种方法来获取数据。

最佳答案

在raw-form.asp中,你可以Response.BinaryWrite Request.BinaryRead的结果,像这样:

<%
If Request.TotalBytes > 0 Then    
    Response.ContentType = "text/plain"
    Response.Write "Your " & Request.ServerVariables("REQUEST_METHOD") & " data was: " 
    Response.BinaryWrite Request.BinaryRead(Request.TotalBytes)
End If
%>

或者您可以使用 Request.BinaryRead,然后将字节写入 ADO 流对象,然后您可以从中读取文本。这是来自的示例:https://stackoverflow.com/a/9777124/989516

<%

If Request.TotalBytes > 0 Then
    Dim lngBytesCount, post
    lngBytesCount = Request.TotalBytes
    post = BytesToStr(Request.BinaryRead(lngBytesCount))
    Response.ContentType = "text/plain"
    Response.Write "Your " & Request.ServerVariables("REQUEST_METHOD") & " data was: " & post
End If

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 = "iso-8859-1"
        BytesToStr = Stream.ReadText
        Stream.Close
    Set Stream = Nothing
End Function

%>

关于vbscript - 使用经典 ASP VBScript 时无法获取原始 POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43237070/

相关文章:

android - 通过 android webview 应用程序登录网络

php - sql查询需要进行哪些改变才能得到结果

windows - 下载VBS错误 "(null): 0x80072EE6"

vbscript - 如何使用 VBScript 获取或检索与当前用户的桌面文件夹不同的用户桌面文件夹?

.net - 在 VBScript (COM-interop) 中确定 .Net 方法后缀号

sql-server - 带有参数化查询的经典 ASP 页面上的 SQL 注入(inject) : text fields

vbscript - Robocopy - 比较文件的修改日期?

SSL 网站在非 SSL (HTTP) 上显示 404 错误

asp-classic - 经典的 asp 混淆

android - 如何使用 Android HTTPPOST jpeg 到 Django 服务器?