发布方法 + WinHttpRequest +多部分/表单数据

发布于 2024-09-26 17:42:13 字数 2970 浏览 4 评论 0原文

我很困惑为什么这不起作用似乎找不到任何问题。

这是代码。

Public Const MULTIPART_BOUNDARY = "speed"
Function getBalance() As String
Dim sEntityBody As String
Dim postBody() As Byte
Dim username As String
Dim password As String

username = CStr(frmMain.txtUser.text)
password = CStr(frmMain.txtPass.text)

sEntityBody = "--" & MULTIPART_BOUNDARY & vbCrLf
sEntityBody = sEntityBody & "Content-Disposition: form-data; name=""function""" & vbCrLf & vbCrLf & "balance" & vbCrLf
sEntityBody = sEntityBody & "--" & MULTIPART_BOUNDARY & vbCrLf
sEntityBody = sEntityBody & "Content-Disposition: form-data; name=""username""" & vbCrLf & vbCrLf & username & vbCrLf
sEntityBody = sEntityBody & "--" & MULTIPART_BOUNDARY & vbCrLf
sEntityBody = sEntityBody & "Content-Disposition: form-data; name=""password""" & vbCrLf & vbCrLf & password & vbCrLf
sEntityBody = sEntityBody & "--" & MULTIPART_BOUNDARY & "--" & vbCrLf

postBody = StrConv(sEntityBody, vbFromUnicode)

Dim xhr As Object
Set xhr = CreateObject("WinHttp.WinHttpRequest.5.1")
xhr.Option(WinHttpRequestOption_EnableRedirects) = False
If xhr Is Nothing Then Set xhr = CreateObject("WinHttp.WinHttpRequest")
If xhr Is Nothing Then Set xhr = CreateObject("MSXML2.ServerXMLHTTP")
If xhr Is Nothing Then Set xhr = CreateObject("Microsoft.XMLHTTP")
xhr.open "POST", "http://poster.example.com", False

xhr.setRequestHeader "User-Agent", "Alalala"
xhr.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & MULTIPART_BOUNDARY
xhr.setRequestHeader "Content-Length", Len(sEntityBody)
xhr.send "" + sEntityBody 'postBody 'URLEncode(sEntityBody)

    If xhr.Status = 200 Then
        getBalance = xhr.responseText
    Else
        frmMain.addToChatbox "Failed at getting response from blah ErrCode:" & xhr.Status
    End If
End Function

现在下面的代码可以工作了(尽管它只是一个 HTML FORM)。

<form 
 method="post" 
 action="http://poster.example.com/" 
 enctype="multipart/form-data">
 <input type="hidden" name="function" value="balance">
 <input type="text"   name="username" value="blah">
 <input type="text"   name="password" value="blah">
 <input type="submit" value="Send">
</form>

这是一个数据包嗅探。 (将主机等更改为示例)

POST / HTTP/1.1..用户代理: 阿拉拉拉..内容类型: 多部分/表单数据; 边界=速度..内容长度: 233..接受:/..主机:poster.example.com..连接: Keep-Alive...--speed..Content-Dispostion:表单数据; 名称=“功能”....平衡..--速度..内容处置: 表单数据; 名称=“用户名”....blah..--速度..内容处置: 表单数据; 名称=“密码”....blah..--速度--..

响应为空

HTTP/1.1 200 OK..日期:10 月 7 日星期四 2010 20:31:20 GMT..服务器: Apache..内容长度:0..连接: 关闭..内容类型:text/html; 字符集=UTF-8......

PS>具有“发送”值的提交按钮不必发送,因此如果有人想知道,那就不是它。 我从嗅探中注意到它可能是什么,它将标头+发布数据(上传数据)作为一个数据包发送,而 firefox/chrome 将其作为 2 个单独的数据包发送。

谢谢

I'm stumped why this doesn't work can't seem to find any problems.

Here is the code.

Public Const MULTIPART_BOUNDARY = "speed"
Function getBalance() As String
Dim sEntityBody As String
Dim postBody() As Byte
Dim username As String
Dim password As String

username = CStr(frmMain.txtUser.text)
password = CStr(frmMain.txtPass.text)

sEntityBody = "--" & MULTIPART_BOUNDARY & vbCrLf
sEntityBody = sEntityBody & "Content-Disposition: form-data; name=""function""" & vbCrLf & vbCrLf & "balance" & vbCrLf
sEntityBody = sEntityBody & "--" & MULTIPART_BOUNDARY & vbCrLf
sEntityBody = sEntityBody & "Content-Disposition: form-data; name=""username""" & vbCrLf & vbCrLf & username & vbCrLf
sEntityBody = sEntityBody & "--" & MULTIPART_BOUNDARY & vbCrLf
sEntityBody = sEntityBody & "Content-Disposition: form-data; name=""password""" & vbCrLf & vbCrLf & password & vbCrLf
sEntityBody = sEntityBody & "--" & MULTIPART_BOUNDARY & "--" & vbCrLf

postBody = StrConv(sEntityBody, vbFromUnicode)

Dim xhr As Object
Set xhr = CreateObject("WinHttp.WinHttpRequest.5.1")
xhr.Option(WinHttpRequestOption_EnableRedirects) = False
If xhr Is Nothing Then Set xhr = CreateObject("WinHttp.WinHttpRequest")
If xhr Is Nothing Then Set xhr = CreateObject("MSXML2.ServerXMLHTTP")
If xhr Is Nothing Then Set xhr = CreateObject("Microsoft.XMLHTTP")
xhr.open "POST", "http://poster.example.com", False

xhr.setRequestHeader "User-Agent", "Alalala"
xhr.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & MULTIPART_BOUNDARY
xhr.setRequestHeader "Content-Length", Len(sEntityBody)
xhr.send "" + sEntityBody 'postBody 'URLEncode(sEntityBody)

    If xhr.Status = 200 Then
        getBalance = xhr.responseText
    Else
        frmMain.addToChatbox "Failed at getting response from blah ErrCode:" & xhr.Status
    End If
End Function

Now this below works (although it's just a HTML FORM).

<form 
 method="post" 
 action="http://poster.example.com/" 
 enctype="multipart/form-data">
 <input type="hidden" name="function" value="balance">
 <input type="text"   name="username" value="blah">
 <input type="text"   name="password" value="blah">
 <input type="submit" value="Send">
</form>

Here is a packet sniff. (Altered the host etc to example after)

POST / HTTP/1.1..User-Agent:
Alalala..Content-Type:
multipart/form-data;
boundary=speed..Content-Length:
233..Accept: /..Host: poster.example.com..Connection:
Keep-Alive....--speed..Content-Dispostion: form-data;
name="function"....balance..--speed..Content-Dispostion:
form-data;
name="username"....blah..--speed..Content-Dispostion:
form-data;
name="password"....blah..--speed--..

response is empty

HTTP/1.1 200 OK..Date: Thu, 07 Oct
2010 20:31:20 GMT..Server:
Apache..Content-Length: 0..Connection:
close..Content-Type: text/html;
charset=UTF-8....

P.S.> The submit button with value Send doesn't have to be sent so thats not it if anyone is wondering.
What it could be and i've noticed from sniffing is that it sends the header + post data (upload data) as one packet and firefox/chrome sends it as 2 seperate packets.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初相遇 2024-10-03 17:42:13

拼写错误的 Content-Disposition 必须是 Content-Disposition 是的,因为我已经受了 6 年的苦了?也许7个小时。

终于解决了

Mis-spelled Content-Dispostion has to be Content-Disposition yup thanks to that i've been suffering for 6? maybe 7 hours.

solved finally

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文