为什么服务器没有收到 multipart/form-data post 数据

Why is Server not receiving multipart/form-data post data

我一直在尝试 post 使用 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 应用程序时正在接收请求,但是它无法识别 posted 对

例如

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

这个returns''表明数据没有被正确接收。

有什么我没有发现的重大缺陷吗?

任何建议都很好。

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

例如

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