I need to do some GETing and POSTing to a RESTful web service from VB6. What is the best and simplest way to do that?
Asked
Active
Viewed 4.8k times
23
-
19My condolences for having to consume a webservice in VB6. – Ryan Tenney Aug 18 '10 at 20:03
-
3@Ryan, At least it is much better than rewriting several classes from php into vb and then maintaining them. And since I'm making the webservice, it is going to be as simple as possible, ie no xml, json, etc. The response will be just a single short string(one word?) or possibly a number. – Echo says Reinstate Monica Aug 18 '10 at 20:15
-
LOL I want to go back to C# (waaaa) LOL – garaber Apr 01 '14 at 18:33
3 Answers
31
You'll need to add a reference to the MSXML library:
Dim sUrl As String
Dim response As String
Dim xmlhttp
Set sUrl = "http://my.domain.com/service/operation/param"
Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "POST", sURL, False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send()
Dim response As String = xmlhttp.responseText
Set xmlhttp = Nothing

Matt Johnson-Pint
- 230,703
- 74
- 448
- 575

Justin Niessner
- 242,243
- 40
- 408
- 536
-
1And conveniently similar to using XMLHTTPRequest in JavaScript inside Internet Explorer! – Ryan Tenney Aug 18 '10 at 20:11
-
@Justin I don't have MSXML2, I do have MSXML.XMLHTTPRequest, is that similar? – Echo says Reinstate Monica Aug 18 '10 at 20:41
-
1That code isn't even close to being VB6, but I suppose you should be able to figure it out from that. – Bob77 Aug 19 '10 at 03:42
-
@Bob Riemersma - You're right. I've spent entirely too much time away from it. Hopefully my edits bring it closer in line to correct VB6 syntax. – Justin Niessner Aug 19 '10 at 12:23
-
How do you pass some data with this code? This snippet just simply calls a service? – giorgio79 Nov 17 '11 at 13:44
-
-
@garaber - You wouldn't. Your `responseText` would just be JSON instead of XML. – Justin Niessner Mar 25 '14 at 17:45
-
-
I need to send the JSON to the server at this point. I have two keys also that I am currently sending as part of the url – garaber Mar 26 '14 at 15:22
-
If createObject function is used, I think there is no need to have reference. Also I don't understand why Server.CreateObject is used, isn't just CreateObject ? – Shafeek Jan 23 '18 at 16:19
-
-
@Shafeek - Here's the link for the classic ASP (VB6) Built-in Object: https://msdn.microsoft.com/en-us/library/ms524786(v=vs.90).aspx – Justin Niessner Jan 24 '18 at 04:01
-
It works!!!!! thanks!!!!.... in my case, there was some missing parenthesis in your code. In the "open" and "header" instruction – César León Sep 21 '18 at 15:54
-
Also, it needs the property "aspcompat="true"" on the page declaration. – César León Sep 21 '18 at 16:04
15
I needed this for GET requests in an old legacy application recently, and since the accepted answer doesn't compile I thought I'd post some working code. I'm sure it will help some poor sole using VB6 in the future ;) Here's a nice clean function.
Public Function WebRequest(url As String) As String
Dim http As MSXML2.XMLHTTP
Set http = CreateObject("MSXML2.ServerXMLHTTP")
http.Open "GET", url, False
http.Send
WebRequest = http.responseText
Set http = Nothing
End Function
And here's example usage:
Dim result As String
Dim url As String
url = "http://my.domain.com/service/operation/param"
result = WebRequest(url)
Happy VB6ing! :)

craftworkgames
- 9,437
- 4
- 41
- 52
-
2lol. I couldn't agree more. It is the [most dreaded language](https://stackoverflow.com/insights/survey/2017/#technology-most-loved-dreaded-and-wanted-languages) in 2017 after all ;) – craftworkgames Apr 06 '17 at 23:52
0
If you need to GET/POST from a REST Web service you can simply write an HTTP Request to the URL of the webservice:
http://www.webservicehost.com/webserviceop?<any parameters>
If you need to pass complex objects you will need to serialize them and then pass them as parameters
You can then get the HTTP Response in whatever format the Web service decides to return as (JSON, XML, etc)

Scott Lance
- 2,239
- 1
- 18
- 19
-
1
-
I think echo knows the principles, and is looking for specific vb6 advice. – MarkJ Aug 18 '10 at 20:03
-
1yup, just the specifics is all I needed. I don't touch VB6 much anymore thankfully, I now work with mostly PHP and some python. – Echo says Reinstate Monica Aug 18 '10 at 20:09
-
Then Justin Niessner has the best answer IMO, sorry for the misunderstanding. – Scott Lance Aug 18 '10 at 20:28