Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Sending and receiving XML or HTML data, communicating with web services using VB6

Published on
13,132 Points
1 Endorsement
Last Modified:
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:
-      URL
-      Headers
-      Data
-      …and Results returned from the web service.
 Screenshot of successful dialing of Cisco phone
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 in new window

Open a connection to the web service:
HttpReq.Open Type_of_Request, URL_of_WebService, Handle_Asynchronously

Open in new window

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)
Next i

Open in new window

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

Open in new window

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)

Open in new window

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 and http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_22503490.html.

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.
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Expert Comment

The zip file is missing "basExampleSendFax.bas"
LVL 11

Author Comment

Thank you XiMaker.  I see I uploaded the wrong file entirely.  That was part of a different test project.  I will upload the correct one now.

The SendFax example referenced in that (incorrectly uploaded) project is part of another project where I am using a MultiTech fax server for incoming and outgoing faxes on the network.  It's only relevant if someone had a MultiTech Fax server.

Again, thanks for pointing this out, and if you have a chance, please take a look at the corrected VB6 project that will be uploaded as "TestWebService_r1.zip" and let me know if you have any feedback.
LVL 11

Author Comment

Thank you!

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Join & Write a Comment

Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month