I’ve seen a number of people looking for examples of how to access web services from VB6. I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web services on my own network, so I thought I’d share this with a few examples as starting points. This is not intended to be a fully working model for communicating with any given web service, but simply a foundation for you to do your own testing within VB6 to communicate with servers that provide web service functionality. There are three starting examples included based on questions I found here on EE:
- Dial a Cisco phone (This is the only example that fully works.)
- Twitter oAuth token request
- eBay service comm
The core of this test harness is the “Microsoft WinHTTP Services” Reference. The main form (frmPostGet), shown below, has the key components in the UI for communicating with a web service:
- …and Results returned from the web service.
The way to use this test harness is to populate the URL, Headers, and Data with service-specific strings, then Send the data and review the results in the “Results” area. Once you have a working process, you can begin to code your own client. The working examples combo box will help fill in some of this information for the included examples.
The [Send] button is where the action takes place. See the attached project for the specific code. It has this sequence of steps:
Initialize the WinHTTPRequest variable:
Set HttpReq = New WinHttpRequest
Open a connection to the web service:
HttpReq.Open Type_of_Request, URL_of_WebService, Handle_Asynchronously
Add headers one row at a time. Each row is separated by CR-LF, then each header name-value pair is separated by a colon. I used an array and the Split() function to easily handle these separations.
arrHeaders = Split(Headers_String, vbCrLf)
For i = 0 To UBound(arrHeaders)
HttpReq.SetRequestHeader Split(arrHeaders(i), ":")(0), Split(arrHeaders(i), ":")(1)
Send the data:
On Error GoTo Error_HTTPSend
HttpReq.Send Data_to_Send 'Note - in many cases, if not using POST or PUT, may need to send only ""
On Error GoTo 0
NOTE: The error trapping is to handle the cases where the web service does not respond (err.number = -2147012889) or any other kind of error.
Finally, I format results into presentable format with CR-LF and push that into the Results field on the form:
txtResults.Text = Replace(HttpReq.ResponseText, vbLf, vbCrLf)
Depending on the web service, you will look in the Results field to find what defines success or failure. In the example shown in the screenshot above, the Cisco phone responded with XML data with one of the elements showing the text “Success” (plus I saw the phone actually dial!)
There are two modules in addition to the main form (basBase64Utils and basURLHelper) that are not essential to the WinHTTP services, but are used to simply aid in the use of the test harness.
basBase64Utils - Web services usually require the authentication user/password to be encoded in Base64 format, so these routines do this coding/decoding. There is also a routine to get a complete file as a Base64 string, for instance to upload to a web service or include as an email attachment. Most of this module is from the author at http://www.nonhostile.com/howto-encode-decode-base64-vb6.asp
basURLHelper – This module is used to easily build and parse the URL field on the form. These routines do the work of determining the protocol, the website URL, and the URI. These are from the authors at http://www.vbforums.com/showthread.php?t=330698
This test harness works with XML and HTML data, since it is using the generic WinHTTP Services to communicate with the web services server. It is up to you to parse the results depending on your specific needs, but there are many tools available online to make this easier.
I hope this helps others as it has me.