Bof*_*ain 2 vbscript http-post asp-classic
我已经尝试了两天来设置满足第三方提供商要求的端点。他们将通过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
%>
Run Code Online (Sandbox Code Playgroud)
raw-form-test1.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>
Run Code Online (Sandbox Code Playgroud)
raw-form-test2.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>
Run Code Online (Sandbox Code Playgroud)
当填写一些随机文本并提交第一个测试时,响应主体符合我的期望:
Your POST data was: data=abc
使用第二项测试时,返回的结果resp为:
200 | Your POST data was:
我也尝试使用Request.BinaryRead()失败(VBScript得到了它的长度,但没有得到它的内容-可能只是VB对类型很糟糕)。我希望有另一种获取数据的方法。
小智 5
在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
%>
Run Code Online (Sandbox Code Playgroud)
或者,您可以使用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
%>
Run Code Online (Sandbox Code Playgroud)