Post Method + WinHttpRequest + multipart/form-data

SSp*_*oke 3 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
Run Code Online (Sandbox Code Playgroud)

现在这下面的工作(尽管它只是一个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>
Run Code Online (Sandbox Code Playgroud)

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

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 = "密码" ......等等..-- speed-- ..

回复是空的

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

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

谢谢

SSp*_*oke 6

错误拼写的内容 - 处置必须是内容 - 处置是的,因为我已经痛苦6?也许7个小时.

终于解决了