Get/post to RESTful web service

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?

Upvotes: 23

Views: 49095

Answers (3)

craftworkgames
craftworkgames

Reputation: 9947

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! :)

Upvotes: 15

Justin Niessner
Justin Niessner

Reputation: 245399

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

Upvotes: 31

Scott Lance
Scott Lance

Reputation: 2249

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)

Upvotes: 0

Related Questions