This article will try to explain the how you can build web services and/or clients with any of the three languages: .NET, MS SOAP Toolkit and Java. But the real point of the article is to show you how you can build clients for web services from any of above-mentioned languages.
Not long ago .NET was released and many of us jumped on to write ASP.NET web sites, C# programs or Web Services. I was really amazed by the relatively easy way someone can write a web service with .NET. I previously had written some web services with MS SOAP Toolkit and Apache SOAP for Java. And then someone asked me to write clients using different languages for those web services. It proved to be not trivial.
Although SOAP is now a standard, different implementations of web services are using it in ways that sometime makes the interoperability with other SOAPs hard if not impossible.
My sample application is a very simple web service with one method: addNumbers. As you already deducted it will add two numbers and return the result. The name of the application is Hello2 and the source files are attached to this message.
STK Service and Clients
First lets write the Web Service using the MS SOAP Toolkit with an ASP listener and an ISAPI listener as well.
The addNumbers method in the Visual Basic class is:
Public Function addNumbers(ByVal NumberOne As Double, ByVal NumberTwo As Double) As Double
addNumbers = NumberOne + NumberTwo
End Function
Using the WSDLGen.exe wizard you can generate the ISAPI listener, the ASP listener or both (not at the same time, of course). I choosed to generate both the ASP and the ISAPI listener, so I named my WSDL files Hello2ASP.WSDL respectively Hello2Isapi.WSDL.
Now lets write some clients for this Hello2 web service.
STK Client
The first client is a Visual Basic client using high level API in SOAP Toolkit. Create a VB project add a form and then a button. The code bellow is executed when the button is hit.
Private Sub cmdDoTest_Click()
Const WS_URL = http://localhost/Hello2/Hello2Isapi.WSDL
Dim objHello2ISapi As SoapClient
Dim nResult As Double, NumberOne As Double, NumberTwo As Double
On Error GoTo catch_err
objHello2ISapi = New SoapClient
Call objHello2ISapi.mssoapinit(WS_URL)
NumberOne = 10
NumberTwo = 25
nResult = objHello2ISapi.addNumbers(NumberOne, NumberTwo)
MsgBox(nResult)
cleanup:
objHello2ISapi = Nothing
Exit Sub
catch_err:
MsgBox(Err.Description)
Resume cleanup
End Sub
As you can see the client is pretty simple and there are no problems. All the details like building the SOAP request message and parsing the result SOAP message is hidden from the programmer.
WS_URL is the URL of the service. The high level API in SOAP Toolkit needs a WSDL file so this URL points to one of the WSDL files. At this point it doesnt matter which one you provide. Though, the performance is better with the ISAPI listener.
Java Client
The second client well write for our Hello2 server is a Java client. I used for this the Apache SOAP 2.1. You can download it for free from
http://xml.apache.org/soap/index.html.
The Java class file for the ASP listener is listed bellow.
import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
import org.apache.soap.transport.http.SOAPHTTPConnection;
public class testClient {
public static void main(String[] args) throws Exception {
URL url = new URL (http://localhost/Hello2/Hello2.asp);
SOAPMappingRegistry smr = new SOAPMappingRegistry ();
StringDeserializer sd = new StringDeserializer ();
smr.mapTypes (Constants.NS_URI_SOAP_ENC, new QName ("", "Result"), null, null,
d);
// create the transport and set parameters
SOAPHTTPConnection st = new SOAPHTTPConnection();
// build the call.
Call call = new Call ();
call.setSOAPTransport(st);
call.setSOAPMappingRegistry (smr);
call.setTargetObjectURI (http://tempuri.org/message/);
call.setMethodName("addNumbers");
call.setEncodingStyleURI (http://schemas.xmlsoap.org/soap/encoding/);
Vector params = new Vector();
params.addElement(new Parameter("NumberOne", Double.class, "10", null));
params.addElement(new Parameter("NumberTwo", Double.class, "25", null));
call.setParams(params);
Response resp = null;
try {
resp = call.invoke (url, http://tempuri.org/action/Hello2.addNumbers);
}
catch (SOAPException e)
{
System.err.println("Caught SOAPException (" + e.getFaultCode () + "): "
e.getMessage ());
return;
}
// check response
if (resp != null && !resp.generatedFault()) {
Parameter ret = resp.getReturnValue();
Object value = ret.getValue();
System.out.println ("Answer--> " + value);
}
else {
Fault fault = resp.getFault ();
System.err.println ("Generated fault: ");
System.out.println (" Fault Code = " + fault.getFaultCode());
System.out.println (" Fault String = " + fault.getFaultString());
}
}
}
As you can see the url variable points to the ASP listener. To point the Java client to the ISAPI listener make the following change:
URL url = new URL ("http://localhost/Hello2/Hello2Isapi.wsdl");
.NET Client
Now it is time to write a .NET client for our Hello2 Web service. Using the WSDL.exe tool from .NET Framework Beta 2 you must generate a proxy class for our service. Execute following command.
wsdl http://localhost/Hello2/Hello2Isapi.wsdl
This will generate Hello2Isapi.cs file. This is the .NET proxy class using C# (this is the default language used). Check out the params for wsdl.exe to generate the proxy using VB.NET or anotherlanguage.
Now compile the proxy using y class to access the Hello2 web service. Here is the code for the C# client.
using System;
public class Hello2ISapiClient
{
public static void Main()
{
Hello2Isapi srv = new Hello2Isapi();
double res = 0, num1 = 10, num2 = 25;
res = srv.addNumbers(num1, num2);
Console.WriteLine("{0}+{1}={2}", num1, num2, res);
}
}
Compile the client using Hello2IsapiClient.cs /reference:Hello2Isapi.dll and run it with Hello2IsapiClient.
At this point we have a MS SOAP Toolkit web service and three clients written with: SOAP Toolkit, Java respectively .NET
Apache SOAP for Java Service and Clients
Lets move on now and write the same service using Apache SOAP for Java. Here is the service:
package samples.MyService;
import java.util.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
public class MyService
{
public double addNumbers(double num1, double num2)
{
return num1+num2;
}
}
I used the name MyService for my service and I added it to the samples package. This way you dont need to add a context into the Tomcat server. Just deploy the service into SOAP using the following deployment descriptor file:
<isd:service xmlns:isd=http://xml.apache.org/xml-soap/deployment
id="urn:myservice-service" checkMustUnderstands="false"> <isd:provider
type="java" scope="Application" methods="addNumbers">
<isd:java class="samples.MyService.MyService" static="false"/>
</isd:provider>
</isd:service>
I wont explain here how to set up Apache SOAP into Tomcat since there is enough guidance in the Apache SOAP documentation.
Apache SOAP Client
Its time to write the clients for this service. The first one is written using Java. Here is the code:
package samples.MyService;
import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
public class client
{
public static void main(String[] args) throws Exception
{
if (args.length != 3
&& (args.length != 4 || !args[0].startsWith("-")))
{
System.err.println("Usage:");
System.err.println(" java " + client.class.getName() +
" [-encodingStyleURI] SOAP-router-URL nameToLookup");
System.exit (1);
}
// Process the arguments.
int offset = 4 - args.length;
String encodingStyleURI = args.length == 4
? args[0].substring(1)
: Constants.NS_URI_SOAP_ENC;
URL url = new URL(args[1 - offset]);
Double num1 = new Double(args[2 - offset]),
num2 = new Double(args[3 - offset]);
SOAPMappingRegistry smr = new SOAPMappingRegistry();
BeanSerializer beanSer = new BeanSerializer();
System.out.println(encodingStyleURI);
System.out.println(url);
System.out.println(num1);
System.out.println(num2);
// Build the call.
Call call = new Call();
call.setSOAPMappingRegistry(smr);
call.setTargetObjectURI("urn:MyService");
call.setMethodName("addNumbers");
call.setEncodingStyleURI(encodingStyleURI);
Vector params = new Vector();
params.addElement(new Parameter("num1", Double.class, num1, null));
params.addElement(new Parameter("num2", Double.class, num2, null));
call.setParams(params);
// Invoke the call.
Response resp;
long nErrors = 0;
Calendar cal = Calendar.getInstance();
Date startTime = cal.getTime(), endTime;
try
{
resp = call.invoke(url, "");
}
catch (SOAPException e)
{
System.out.println("i=" + i);
System.err.println("Caught SOAPException (" +
e.getFaultCode() + "): " +
e.getMessage());
return;
}
// Check the response.
if (!resp.generatedFault())
{
Parameter ret = resp.getReturnValue();
Object value = ret.getValue();
//System.out.println(value != null ? "\n" + value : "I don't know.");
}
else
{
Fault fault = resp.getFault();
System.err.println("Generated fault: ");System.out.println (" Fault Code = " + fault.getFaultCode()); System.out.println (" Fault String = " + fault.getFaultString
));
}
cal = Calendar.getInstance();
endTime = cal.getTime();
System.out.println("Start time="+startTime);
System.out.println("End time="+endTime);
System.out.println ("Errors=" + nErrors);
}
}
As you can see the code is pretty straightforward. No problems expected since the same SOAP library is used.
The code for a STK client is bellow:
STK Client
There are errors in both high level and low level clients because xsi:type is required in Apache SOAP for Java.
.NET Client
Due to the same issue a .NET client wont work either.
.NET Service and Clients
.NET Framework Beta 2 is the newest technology and changes are expected to happen from Betza 2 to the final release. Major changes already have been made when Beta 2 was released. This is no surprise since Microsoft had warned developers that might happen.
Writing a web service with .NET is very simple and can be done in several ways. I choose to write my web service in C# using a ASMX file. Here is the content of the file.
<%@ WebService Language="C#" Class="MyService" %>
using System;
using System.Web.Services;
[WebService(Namespace=http://www.catalin.com/webservices/)]
public class MyService: WebService
WebMethod(Description="return the sum of two numbers")]
[System.Web.Services.Protocols.SoapRpcMethodAttribute(
http://www.catalin.com/webservices/addNumbers,
RequestNamespace="http://www.catalin.com/webservices/",
ResponseNamespace=http://www.catalin.com/webservices/)]
public double addNumbers(double numberOne, double numberTwo)
{
return numberOne + numberTwo;
}
}
The advantage of using a ASMX file is that no compilation is needed so a hot deployment can be done.
Place the file into a virtual directory under IIS. You can test the service with your IE with http://localhost/testdotnetws/myservice.asmx
.NET Client
Writing a client for this service is similar with the .NET client we write earlier. When generating the proxy file specify the WSDL file with http://localhost/testdotnetws/myservice.asmx?WSDL. This is the way .NET framework creates the WSDL file on-demand.
STK Client
Using the high level API would be a faster solution but there are some issues there and I couldnt solve them out so I used the low level API. The client code is not difficult. The only trick is to enable the .NET service for RPC type of calls. Thanks to Christian Weyer who helped me with this issue.
Look at the web service code and notice the System.Web.Services.Protocols.SoapRpcMethodAttribute attribute for our method. Without this attribute the default type of conversation in .NET is message.
Java Client
In the java client youll have to modify the url as follows:
URL url = new URL ("http://localhost/aspnet_test/myservice/myservice.asmx");
Compile and run.
I hope this short trip to the web service world was helpful for those of you who are involved in web services development.
Happy SOAP-ing!