Create VB and C++ Web Service Clients
After deploying the Java Web service, you can test SOAP's cross-language capabilities by creating clients in other languages that call the
sayHello() method exposed by the Java service. Each client passes a SOAP message containing a string parameter and receives a "
Hello xyz" string in response (where "
xyz" is the value of the string parameter passed by the client).
The first step is to create a VB client class capable of constructing the appropriate SOAP messages using the WSDL file as a model. I used the Cape Clear CapeStudio WSDL Assistant. The CapeStudio WSDL Assistant tool takes a WSDL file as input and creates a VB class (cls) file that acts as a proxy and lets you treat the Web service just as you would any other COM object. The generated class transparently creates and parses the SOAP message call and response. The Microsoft SOAP Toolkit generates WSDL files only for Microsoft COM components, and it doesn't allow you to generate VB cls files by specifying a WSDL file-but no SOAP toolkit possesses this feature.
|Figure 1: The VB 6 IDE. The VB 6 IDE with the References dialog open.|
Next, create an ActiveX DLL containing the generated
file. I used Visual Basic 6.0 to create the DLL file because putting it in a separate file makes the class reusable, as you'll see later in this article.
In the VB IDE, click the File menu, select New Project, and choose the ActiveX DLL project type. Name the project HelloVB. Create a new class module named
Hello_PortTypeClient and copy the generated
cls file code into the new class. Now select References from the Project Menu. Figure 1 shows the References dialog.
Select the Microsoft SOAP Type library and Microsoft XML, v3.0 from the references.
|Author Note: You will not see the Microsoft SOAP type library and Microsoft XML, v3.0 references if you have not installed the Microsoft SOAP Toolkit on your machine. Any client machine running the ActiveX DLL must have the Microsoft SOAP Toolkit installed as well, because the uses Microsoft SOAP to communicate with the Web service.|
After including the two required references, select "Make HelloVB.DLL" to create the DLL file.
Now you can create a very simple Standard EXE application that uses the DLL to communicate with the Web service. Close your DLL project and select File, New Project, and choose the Standard EXE project type. To use the HelloVb.DLL, select References from Project Menu and then select
HelloVb from the reference list. The form itself contains only the code to create a Hello_PortTypeClient object, make the call to the
sayHello() method of the Java Web service, and display the results.
' in the Declarations section
Dim x As HelloVb.Hello_PortTypeClient
Private Sub Form_Load()
Set x = New Hello_PortTypeClient
|Figure 2: The Visual Studio IDE. The Visual Studio IDE with the open MFC ClassWizard dialog. Start with this dialog to add the HelloVB ActiveX Dynamic Link Library.|
Before running this program, make sure to start the server on which you deployed the Java service. When you run the VB application, it displays a message box containing "Hello VB", the return value of the Java service.
As you know, DLLs are portable and reusable, so you can use the same VB DLL to call the Web service from a VC++ program. Briefly, here's how to instantiate objects and call a VB ActiveX DLL from VC++ (I used Visual C++ version 6.0):
- The VC++ project is a simple Single Document Interface (SDI) executable, MFC-based application. After you create the VC++ project with the wizard, you need to add the VB Dynamic Link Library to it.
- Open the ClassWizard dialog (Ctrl+W) and then add the class by selecting the type library (see Figure 2 and Figure 3).
This procedure adds a class wrapper for the VB DLL class module. Now include "HelloVb.h" in your project header file (in the sample code, the header file is
HelloFromVC2.h). Also declare a VB DLL class module object pointer, for example:
Hello_PortTypeClient * mu_pMainClass
|Figure 3: Select the VB DLL file (HelloVb.dll). When the next dialog box appears, you can select the classes in the HelloVB.dll that you want to add to the project.|
Next initialize OLE (see lines 53-57 in HelloFromVC2.cpp) and create the VB DLL class module object (see lines 79-109 in HelloVC2.cpp). Finally, you can call the Web service. To initiate the call with the sample application, click the Tools menu and select the "Call HelloVb" item. The menu fires a
method which calls the
VB DLL sayHello()
method with a string parameter, which then creates the SOAP message and calls the
method of the Java Web service.
So, now you've seen how to leverage the power of SOAP in a language-independent manner. You could just as easily develop your Web service in VB or a .NET language, and call it from a Java client. SOAP is simpler and easier to implement than any existing alternative, and it makes better use of the pervasive Web infrastructure. The overall result is that you can write code to pull a system together in weeks rather than annual quarters.