3

How to send POST request to remote URL using VB6 ?

1
  • There are still people using VB6? :o Nov 2, 2010 at 21:23

3 Answers 3

5

Many ways to approach this. You can use WinInet API, WinHTTP API, WinHTTPRequest, or XMLHTTPRequest. I prefer the lower leveled Winsock, and you can read about it here: http://www.vbforums.com/showthread.php?t=334645 . Winsock is a bit more complicated, and but a bit more powerful, in my opinion. If you want to do it simple and sweet, XML HTTP Request is the way to go, I use it in javascript too. Try something like:

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

And the OnReadyStateChange function:

Dim HttpResponse As String

HttpResponse = myMSXML.responseText

If you find my code not working, or you're a bit confused, I'm sorry, I'm a bit rusty with VB nowadays. You can check out the official Microsoft documentation on XMLHTTPRequest here: http://msdn.microsoft.com/en-us/library/ms759148%28VS.85%29.aspx

1
  • +1 Similar to the code posted in this question which is a good sign.
    – MarkJ
    Nov 3, 2010 at 11:57
5

We can do this way also

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

More references you can check http://smartreferences.blogspot.in

1

For my API worked only with "application/json" in "Content-Type" header. Here is my code:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.