views:

55

answers:

3

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?

A: 

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
Your answer doesn't address how to implement this in VB6.
Ryan Tenney
I think echo knows the principles, and is looking for specific vb6 advice.
MarkJ
yup, 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
Then Justin Niessner has the best answer IMO, sorry for the misunderstanding.
Scott Lance
+5  A: 

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
Justin Niessner
wonderful, just what I was looking for.
Echo
And conveniently similar to using XMLHTTPRequest in JavaScript inside Internet Explorer!
Ryan Tenney
@Justin I don't have MSXML2, I do have MSXML.XMLHTTPRequest, is that similar?
Echo
@Echo - Yes. That should work just as well.
Justin Niessner
That code isn't even close to being VB6, but I suppose you should be able to figure it out from that.
Bob Riemersma
@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
fully got it working
Echo
A: 

You can create an HttpWebRequest object:

Dim req As HttpWebRequest = HttpWebRequest.Create("http://url.to/restful/service");
req.Method = "POST" 'or "GET"

'Specify the data to send in the request
Dim text As String
text = "Data to send in request"

req.ContentType = "application/x-www-form-urlencoded" 'The type of content youre sending
req.ContentLength = CLng(text.Length) 'The length of content youre sending

'Get the request stream
Dim s As Stream
s = req.GetRequestStream()

'Write data to request stream
Dim sw As StreamWriter = New StreamWriter(s)
sw.Write(text)

'Clean up
sw.Close()
s.Close()

'Get response from request
Dim res As HttpWebResponse = req.GetResponse()

'Get output stream from response
Dim sr As StreamReader = new StreamReader(res.GetResponseStream)

'Clean up
sr.Close()
res.Close()
req = Nothing
res = Nothing
Donald
Your code example is VB.NET. The OP specifically asked for a VB6 method.
Justin Niessner
HttpWebResponse is specific to .NET, OP is using VB6.
Ryan Tenney