2015-01-14 51 views
0

我一直試圖使用多部分/表單數據發佈數據到服務器,但服務器似乎並沒有收到任何東西。爲什麼服務器沒有收到多部分/表單數據發佈數據

VB代碼

' create a boundary consisting of a random string 
strBoundary = RandomAlphaNumString(32) 

strBody = "--" & strBoundary & vbCrLf 
strBody = strBody & "Content-Disposition: form-data; name=""test1""" & vbCrLf & vbCrLf & STRING 
strBody = strBody & vbCrLf & "--" & strBoundary & vbCrLf 
strBody = strBody & "Content-Disposition: form-data; name=""data""" & vbCrLf & vbCrLf & data 
strBody = strBody & vbCrLf & "--" & strBoundary & vbCrLf 
strBody = strBody & "Content-Disposition: form-data; name=""data2""" & vbCrLf & vbCrLf & data2 
strBody = strBody & vbCrLf & "--" & strBoundary & vbCrLf 
strBody = strBody & "Content-Disposition: form-data; name=""data3""" & vbCrLf & vbCrLf & data3 
strBody = strBody & vbCrLf & "--" & strBoundary & "--" 

' Content-Length 
sHttpLength = Len(strBody) 

Set WinHttpReq = New WinHttpRequest 
strURL = "https://" & HOST & URL ' directed to test.php 

hostHeader = HOST & vbCrLf 
contentTypeHeader = "multipart/form-data; boundary=" & strBoundary & vbCrLf 
contentLengthHeader = sHttpLength & vbCrLf & vbCrLf 

    WinHttpReq.Open "POST", strURL, False 'Open a Http connection 
    WinHttpReq.SetRequestHeader "HOST", hostHeader 
    WinHttpReq.SetRequestHeader "Content-Type", contentTypeHeader 
    WinHttpReq.SetRequestHeader "Content-Length", contentLengthHeader 

WinHttpReq.Send strBody ' Send Post messages 

服務器接收到該請求,因爲它的數據發送回動的應用程序但它不識別張貼對

例如

$postedVal = isset($_POST["test1"]) ? $_POST["test1"] : ''; 

這會返回'',表示數據未被正確接收。

我沒有看到任何重大缺陷嗎?

任何建議將是偉大的。

回答

0

事實證明,如果您沒有在content-Type標頭中指定字符集,它會自動在標頭末尾分配UTF-8。這會導致發佈的消息無法使用!解決方案在邊界之前手動輸入字符集。現在它工作正常.....當你不斷檢查邊界時發現嚴重錯誤!

E.g.

contentTypeHeader = "multipart/form-data;Charset=UTF-8; boundary=" & strBoundary & vbCrLf 
相關問題