Metodo Post + WinHttpRequest + multipart / form-data
-
28-09-2019 - |
Domanda
stumped perché questo non lavoro può non sembrano trovare alcun problema.
Ecco il codice.
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
Ora, questo seguito opere (anche se è solo una forma 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>
Ecco un annusare pacchetto. (Altered l'host ecc per esempio dopo)
POST / HTTP / 1.1..User-Agent: Alalala..Content-Type: multipart / form-data; boundary = speed..Content-Lunghezza: 233..Accept: / .. Host: poster.example.com..Connection: Keep-Alive ....-- speed..Content-dispostion: form-data; name = "funzione" .... equilibrio ..-- speed..Content-dispostion: sagomata dati; name = "username" .... bla ..-- speed..Content-dispostion: sagomata dati; name = "password" .... bla ..-- velocità - ..
risposta è vuoto
HTTP / 1.1 200 OK..Date: Thu, 7 ottobre 2010 20:31:20 GMT..Server: Apache..Content-Lunghezza: 0..Connection: close..Content-Type: text / html; charset = UTF-8 ....
P.S.> Il pulsante con valore Invia presentare non deve essere inviato in modo non thats se qualcuno si sta chiedendo. Che cosa potrebbe essere e ho notato da sniffing è che si invia l'intestazione + dati post (dati upload) come un pacchetto e Firefox / Chrome invia come 2 pacchetti separati.
Grazie
Soluzione
Mis-farro Content-Disposition deve essere Content-Disposition yup grazie a quel Ho sofferto per 6? forse 7 ore.
risolto infine