发布方法+ WinHttpRequest + multipart/form-data [英] Post Method + WinHttpRequest + multipart/form-data
问题描述
我很困惑为什么这不起作用似乎无法发现任何问题.
I'm stumped why this doesn't work can't seem to find any problems.
这是代码.
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表单).
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: 多部分/表单数据; 边界=速度..内容长度: 233..Accept:/ .. Host:poster.example.com..Connection: 保持活动...--速度..内容发布:表单数据; name ="function" .... balance ..-- speed..Content-Dispostion: 表格数据 name ="username" ....等等...--速度..Content-Dispostion: 表格数据 name ="password" ....等等---速度-..
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--..
响应为空
HTTP/1.1 200 OK ..日期:10月7日,星期四 2010 20:31:20 GMT ..服务器: Apache..Content-Length:0..Connection: close..Content-Type:text/html; charset = UTF-8 ....
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.>不必发送带有值Send的提交按钮,所以如果有人想知道的话就不用发送了. 可能是什么,我从嗅探中注意到,它以一个数据包的形式发送标头+帖子数据(上传数据),而Firefox/chrome以2个独立的数据包的形式发送它.
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.
谢谢
推荐答案
拼写错误的 Content-Dispostion 必须是 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.
终于解决了
这篇关于发布方法+ WinHttpRequest + multipart/form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!