为什么服务器不接收多部分/表单数据发布数据

时间:2015-01-14 10:08:11

标签: vb6 multipartform-data winhttprequest

我一直试图使用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应用程序但是它不识别已发布的对

e.g。

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

这会返回''显示数据未正确接收。

我没有看到任何重大缺陷吗?

任何建议都会很棒。

1 个答案:

答案 0 :(得分:0)

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

E.g。

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