Calling a Web Service using VB6 with SOAP 3.0

This article shows you how to create a client that calls a web service from Visual Basic 6 using SOAP 3.0. If you are still using VB 6.0 or C++ and don't plan to move into VB.NET, you will find this method very useful.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 83
April 12, 2006
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

Introduction

In an earlier article the method of calling a web service from a client created using Microsoft Visual Studio 2003 IDE was described. The IDE offers an excellent method to get a reference to the necessary proxy by way of the WSDL utility. This is carried out by adding a web reference to the client application.

This article shows how you may create a client that calls the web service from Visual Basic 6 using Soap 3.0. By providing Soap 3.0,  Microsoft provides VB programmers with the ability to tap web services. This is most useful to those who are still using VB 6.0 or C++ and are not contemplating a move into VB.NET.

This tutorial assumes that web services have been created using any of the programming languages and that a wsdl web reference is available. In particular it creates clients for the web services created using the ColdFusion service with just one method, and another web service using VB.NET with two methods. Examples of early binding and late binding are also shown for the web service created with ColdFusion.

SOAP basics

SOAP, which stands for Simple Object Access Protocol, is a vendor- and platform-agnostic protocol which uses XML and HTTP (SOAP Specification 1.0) to access resources (servers, services and objects) through firewalls in a distributed environment. Later SOAP specifications do not mandate the use of HTTP,  and other protocols such as SMTP and FTP can also be used.  Please review SOAP and other related protocols at the W3C website and numerous other links at that site.

The web services used in the tutorial

The following are the web services for which client applications call the services using VB 6.0 code. Herein two of the services considered in earlier tutorials will be used.

The first service to be called is a web service created using ColdFusion. The details of this service are described in a tutorial that will appear in the future in DevArticles.com with the title Creating a Web Service with ColdFusion: the Basics. In brief, a registered CFC (ColdFusion Component)  which is based on a ColdFusion function shown below is implemented as the web service:

<cfcomponent>
<cffunction name="WelcomeMsg" access="remote" returntype="string" 
output="no"> <cfargument name="name" type="string" required="yes"> <cfreturn "<h3><font color='blue'>Welcome to my Web Service Site
</font></h3> " & arguments.name &"! " & "What would you like to do?"> </cffunction> </cfcomponent>

In the above service the web service method name is WelcomeMsg(). It just takes one argument, which is a string. The service will return a html fragment after processing and adding some tags.

The client coding with VB6, example 1: Calling a ColdFusion web service

The first thing to do is create a standard EXE project. In this case a generic client is declared, and this is set using the CreateObject() function with the object Id being the MSSoapClient.

Add a text box and a command button to your form. To the click event of the command button add the following code. This is the case of late binding, as no type library is referenced. You may check that in the object browser.

Private Sub Command1_Click()
Dim client
Set client = CreateObject("MSSOAP.SoapClient30")
oClient.MSSoapInit _ "http://localhost/CF_WebSvc/justFunction.cfc?wsdl"

strg = (oClient.WelcomeMsg(Text1.Text))
MsgBox (strg)

If Err.Number = -2147024809 Then
MsgBox "Invalid Web Service Request. Please check the Web Service URL"
End If

End Sub

It is assumed that you have the service hosted on the localhost at the designated directory. Make sure that your web reference is showing a valid WSDL. The question is, how does one know that the client has a method called WelcomeMsg()? This is where the WSDL can be used to advantage as it will show both the request sent and the responses expected, as well as the methods called. Please review earlier tutorials.

When you run the above code you should get the following response from the web service.

Example 2: Calling a ColdFusion web service

In this example the same web service is called, but with the New construct. In order to use SOAP 3.0 with this method, you should access the Microsoft SOAP Type Library 3.0, which adds the MSSOAP30.dll (the version used in this tutorial is 3.0.1325.0) . You can access this by going to Project-->References from the main menu. If you do not see this, then you will have to download it from the Microsoft website. It is also installed with Office 2003 (Office 11). Sometimes you may have to browse if it is located in another path with the Browse...button.

With the type library added you can access the various objects, methods and events in the object browser as shown. It has many classes as shown here, of which the class of interest at present is the SoapClient30.

 If you forgot to add this reference and tried to run the code, you would get a compiler error message as follows:

To form1 of a new project, add a button and a text box after adding the reference. Now review the code for calling the service shown below, added to the click event.

Private Sub Command1_Click()
Dim clnt As New SoapClient30
Dim strsvc As String
clnt.MSSoapInit "http://localhost/CF_WebSvc/justFunction.cfc?wsdl"
strsvc = clnt.WelcomeMsg(Text1.Text)
MsgBox (strsvc)
End Sub

Because you have established a reference you get the advantage of intellisense, and as you type in the code you get the cue to the appropriate property, method, or event to choose. Now if you execute the above code, you get the same kind of reply as before, since nothing else has changed.

Example 3: SOAP client calling the Enc/Denc web service

The second web service considered is a service that has two defined methods. The service takes a string and returns its encoded value as the first method (Enc()) and the second method (denc()) takes a given value and returns its de-encoded value. Please review this tutorial regarding this service. The web service code is created using VB.NET and summarized in the following code listing:

Imports System.Web.Services
<System.Web.Services.WebService(Namespace := "http://tempuri.org/
Hencode/Service1")> _ Public Class Service1 Inherits System.Web.Services.WebService #Region " Web Services Designer Generated Code " Public Sub New() MyBase.New() 'This call is required by the Web Services Designer. InitializeComponent() 'Add your own initialization code after the InitializeComponent()
call End Sub 'Required by the Web Services Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Web Services Designer 'It can be modified using the Web Services Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent() components = New System.ComponentModel.Container() End Sub Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) 'CODEGEN: This procedure is required by the Web Services Designer 'Do not modify it using the code editor. If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub #End Region Public Function enc (ByVal StrEncode As String) Dim encodedStr As String encodedStr = _ Convert.ToBase64String(System.Text.ASCIIEncoding. _ ASCII.GetBytes(StrEncode)) Return (encodedStr) End Function <WebMethod()> _ Public Function denc (ByVal StrDecode As String) Dim decodedStrg As String decodedStrg = System.Text.ASCIIEncoding.ASCII. _ GetString(Convert.FromBase64String(StrDecode)) Return decodedStrg End Function End Class

Create a project and a command button to the default form. To the click event of the button add the code shown here. The client once declared can be used for all the methods the service provides.

Private Sub Command1_Click()
Dim clnt As New SoapClient30
Dim strsvc As String
'the client is initialized
clnt.MSSoapInit "http://localhost/Hencode/Service1.asmx?wsdl"
'the client calls the enc method of the service
strsvc = clnt.Enc ("Mysorian")
'the result returned from the web service is sent to message box
MsgBox (strsvc)
Dim orig As String
'the client is called now with denc method of the web service
'the denc takes the string produced by the first call to enc
'the idea is to reproduce the original string
orig = clnt.denc(strsvc)
MsgBox (orig)
End Sub

If you run the above code, you will first get an encoded value, and in the second message your original is recovered as shown.

Summary

SOAP 3.0 will be used by those who cannot afford, or do not want to implement, the .NET framework as the programming backbone. It is also quite useful to those developers who want to use their VB 6 skills. The method does not have the excellent support of the built-in web service discovery tool in VS 2003, but necessary information may be obtained from the web reference (WSDL).  It is possible to use either of the CreateObject() or NEW instantiation schemes.

blog comments powered by Disqus
VISUAL BASIC.NET ARTICLES

- Basic Form Properties and Modality in VB.NET
- Multiple Document Interfaces in Visual Basic
- Visual Basic for Beginners
- ASP.NET Image to PDF with VB.Net
- MySQL in ASP.NET: Mono using VB.NET
- AsyncFileUpload File Type and File Size Vali...
- Visual Studio: Adding Functionality and Style
- Clocks and Countdowns
- User-defined Functions using Visual Basic Ap...
- Understanding Object Binding in VBA
- Mastering the Message Box
- Testing a Windows Forms Application
- Using Visual Basic.NET Features to Code a Wi...
- Correcting Code in a Windows Forms Applicati...
- Write Readable Code and Comments for Windows...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials