如何将POST发送到远程URL?

时间:2010-11-02 21:05:52

标签: vb6

如何使用VB6将POST请求发送到远程URL?

3 个答案:

答案 0 :(得分:5)

我们也可以这样做

Set myMSXML = CreateObject("Microsoft.XmlHttp")
myMSXML.open "POST", "http://....", False
myMSXML.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
myMSXML.setRequestHeader "User-Agent", "Firefox 3.6.4"
myMSXML.send "param1=value2&param2=value2"
MsgBox myMSXML.responseText

您可以查看更多参考资料http://smartreferences.blogspot.in

答案 1 :(得分:4)

有很多方法可以解决这个问题。您可以使用WinInet API,WinHTTP API,WinHTTPRequest或XMLHTTPRequest。我更喜欢较低水平的Winsock,你可以在这里阅读: http://www.vbforums.com/showthread.php?t=334645。在我看来,Winsock有点复杂,但有点强大。如果你想要简单而甜蜜,XML HTTP Request是要走的路,我也在javascript中使用它。尝试类似:

Set myMSXML = New MSXML.XMLHTTPRequest
myMSXML.open "POST", URL, True
myMSXML.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
myMSXML.setRequestHeader "User-Agent", "Firefox 3.6.4"
myMSXML.OnReadyStateChange = (Shown below)
myMSXML.send YourPostDataString

OnReadyStateChange函数:

Dim HttpResponse As String

HttpResponse = myMSXML.responseText

如果你发现我的代码不起作用,或者你有点困惑,我很抱歉,现在我对VB有点生疏了。您可以在此处查看有关XMLHTTPRequest的官方Microsoft文档:http://msdn.microsoft.com/en-us/library/ms759148%28VS.85%29.aspx

答案 2 :(得分:0)

对于我的API,仅在“ Content-Type”标头中使用“ application / json”。 这是我的代码:

textJSON = "{ ""field1"":""value1"", ""field2"":""value2""}"
Set myMSXML = CreateObject("Microsoft.XmlHttp")
myMSXML.Open "POST", "http://...", False
myMSXML.setRequestHeader "Content-Type", "application/json"
myMSXML.setRequestHeader "User-Agent", "Firefox 3.6.4"
myMSXML.send textJSON
MsgBox myMSXML.responseText