0

我一直在尝试使用 multipart/form-data 将数据发布到服务器,但是服务器似乎没有收到任何内容。

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

服务器正在接收请求,因为它将数据发送回 vb 应用程序,但它无法识别已发布的对

例如

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

这返回 '' 表明数据没有被正确接收。

有什么我没有看到的重大缺陷吗?

任何建议都会很棒。

4

1 回答 1

0

事实证明,如果您没有在 content-Type 标头中指定 Charset,它会在标头末尾自动分配 UTF-8。这导致发布的消息不起作用!解决方法是在边界前手动输入Charset。现在它工作正常.....当您继续检查边界时很难找到错误!

例如

contentTypeHeader = "multipart/form-data;Charset=UTF-8; boundary=" & strBoundary & vbCrLf
于 2015-01-19T14:17:39.417 回答