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
2010-08-18 19:57:17
Your answer doesn't address how to implement this in VB6.
Ryan Tenney
2010-08-18 20:01:34
I think echo knows the principles, and is looking for specific vb6 advice.
MarkJ
2010-08-18 20:03:44
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
2010-08-18 20:09:40
Then Justin Niessner has the best answer IMO, sorry for the misunderstanding.
Scott Lance
2010-08-18 20:28:44
+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
2010-08-18 19:59:00
And conveniently similar to using XMLHTTPRequest in JavaScript inside Internet Explorer!
Ryan Tenney
2010-08-18 20:11:04
@Justin I don't have MSXML2, I do have MSXML.XMLHTTPRequest, is that similar?
Echo
2010-08-18 20:41:13
That code isn't even close to being VB6, but I suppose you should be able to figure it out from that.
Bob Riemersma
2010-08-19 03:42:11
@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
2010-08-19 12:23:16
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
2010-08-18 20:09:50
Your code example is VB.NET. The OP specifically asked for a VB6 method.
Justin Niessner
2010-08-18 20:12:30