Vb6 http post request on windows XP

旧城冷巷雨未停 提交于 2021-02-07 20:51:16

问题


I have a problem sending POST request with VB6. The code below works properly on Windows7 but on Windows XP it run without any runtime error and it sends the packet but looks like it doesn't append the post data in the packet. My code is like this:

Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "POST", url, False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.setRequestHeader "Content-Length", Len(parameters)
xmlhttp.Send parameters

where paramaters contains the string "bar=foo&foo=bar"

I already tried to add the references to Microsoft XML, v4.0.


回答1:


I found a solution. I changed the code in this way:

Dim xmlhttp As WinHttp.WinHttpRequest 
...

Set xmlhttp = New WinHttp.WinHttpRequest

xmlhttp.open "POST", url, False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.setRequestHeader "Content-Length", Len(parameters)
xmlhttp.Send parameters

Adding the reference to "Microsoft WinHTTP Services, version 5.1"

And now it works.




回答2:


Just guessing here but try changing this line by adding 10 (or 100) to the length. change this xmlhttp.setRequestHeader "Content-Length", Len(parameters) to this xmlhttp.setRequestHeader "Content-Length", Len(parameters) + 10

I have never been told why I should do this, just that I should add 10 or more to the length.




回答3:


Have you tried putting brackets arround the send parameter like this?

Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP") xmlhttp.open "POST", url, False      
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
xmlhttp.setRequestHeader "Content-Length", Len(parameters) 
xmlhttp.Send (parameters)

What I think is happening is that because you are sending the parameter ByRef the ServerXMLHTTP object is getting confused when picking the correct overload of the Send method. It thinks you are sending a pointer to an IStream when in fact you are trying to send is a BSTR. By putting the parameter in brackets it makes the compiler send the variable ByVal instead of ByRef and thus the compiler realises you are not sending a pointer and selects the correct overload of the Send function.



来源:https://stackoverflow.com/questions/9942787/vb6-http-post-request-on-windows-xp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!