Post Method + WinHttpRequest + multipart / form-data

时间:2010-10-07 20:39:48

标签: vb6 multipartform-data winhttprequest

我很难过为什么这不起作用似乎无法找到任何问题。

这是代码。

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 
 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..User-Agent:   Alalala..Content型:   多部分/格式的数据;   边界= speed..Content-长度:   233.Accept: / ..主持人:poster.example.com..Connection:   Keep-Alive ....-- speed..Content-Dispostion:form-data;   NAME = “功能” ....平衡..-- speed..Content-Dispostion:   形式数据;   NAME = “用户名” ....等等..-- speed..Content-Dispostion:   形式数据;   NAME = “密码” ....等等..--速度 - ..

回复是空的

  

HTTP / 1.1 200 OK ..日期:星期四,10月7日   2010 20:31:20 GMT..Server:   Apache..Content-Length:0..Connection:   close..Content-Type:text / html;   字符集= UTF-8 ....

P.S.&GT;不需要发送带有值Send的提交按钮,如果有人想知道那就不是它。 可能是什么,我从嗅探中注意到,它将标题+发布数据(上传数据)作为一个数据包发送,firefox / chrome将其作为2个单独的数据包发送。

谢谢

1 个答案:

答案 0 :(得分:6)

错误拼写内容处理必须 Content-Disposition 是的,因为我已经痛苦了6岁?也许7个小时。

终于解决了