Post方法+ WinHttpRequest + multipart / form

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

这是代码。

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..User-Agent:Alalala..Content-Type:multipart / form-data; 边界=速度..内容长度:233 ..接受:/ ..主机:poster.example.com。连接:保持活动....--速度..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.Date:Thu,07 Oct 2010 20:31:20 GMT..Server:Apache..Content-Length:0..Connection:close..Content-Type:text / html; 字符集= UTF-8 ....

PS>具有值Send的提交按钮不需要发送,所以如果有人想知道的话不需要发送。 它可能是,我从嗅探注意到,它发送标题+发布数据(上传数据)作为一个数据包和Firefox /铬发送它作为2个独立的数据包。

谢谢


拼写错误的Content-Dispostion必须是Content-Disposition是因为我已经忍受了6次了吗? 也许7个小时。

最后解决

链接地址: http://www.djcxy.com/p/22209.html

上一篇: Post Method + WinHttpRequest + multipart/form

下一篇: What is the boundary in multipart/form