Web Services between .NET, Java and MS SOAP Toolkit

 

This article will try to explain the how you can build web servicesand/or clients with any of the three languages: .NET, MS SOAP Toolkitand Java. But the real point of the article is to show you how you canbuild clients for web services from any of above-mentioned languages.

Not long ago .NET was released and many of usjumped on to write ASP.NET web sites, C# programs or Web Services. Iwas really amazed by the relatively easy way someone can write a webservice with .NET. I previously had written some web services with MSSOAP Toolkit and Apache SOAP for Java. And then someone asked me towrite clients using different languages for those web services. Itproved to be not trivial. Although SOAP is now a standard, differentimplementations of web services are using it in ways that sometimemakes the interoperability with other SOAPs hard if not impossible.

My sample application is a very simple webservice with one method: addNumbers. As you already deducted it willadd two numbers and return the result. The name of the application isHello2 and the source files are attached to this message.

STK Service and Clients
First let�s 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, theASP listener or both (not at the same time, of course). I choosed togenerate both the ASP and the ISAPI listener, so I named my WSDL filesHello2ASP.WSDL respectively Hello2Isapi.WSDL.

Now let�s write some clients for this Hello2 web service.

STK Client
The first client is aVisual Basic client using high level API in SOAP Toolkit. Create a VBproject add a form and then a button. The code bellow is executed whenthe 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
Set objHello2ISapi = New SoapClient
Call objHello2ISapi.mssoapinit(WS_URL)

NumberOne = 10
NumberTwo = 25
nResult = objHello2ISapi.addNumbers(NumberOne, NumberTwo)
MsgBox nResult

cleanup:
Set objHello2ISapi = Nothing
Exit Sub

catch_err:
MsgBox Err.Description
Resume cleanup
End Sub

As you cansee the client is pretty simple and there are no problems. All thedetails like building the SOAP request message and parsing the resultSOAP message is hidden from the programmer.WS_URL is the URL of the service. The high level API in SOAP Toolkitneeds a WSDL file so this URL points to one of the WSDL files. At thispoint it doesn�t matter which one you provide. Though, the performanceis better with the ISAPI listener.

Java Client
The second client we�ll write for our Hello2 server is a Java client. Iused for this the Apache SOAP 2.1. You can download it for free fromhttp://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, sd);

// 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 cansee the url variable points to the ASP listener. To point the Javaclient to the ISAPI listener make the following change:

URL url = new URL ("http://localhost/Hello2/Hello2Isapi.wsdl");

.NET Client
Now it is time to writea .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. Thisis 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 oranother language.Now compile the proxy using

csc.exe /t:library Hello2Isapi.cs

It�s time to write the .NET client, which usesthe proxy class to access the Hello2 web service. Here is the code forthe 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:Hell
o2Isapi.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
Let�s 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 thename MyService for my service and I added it to the samples package.This way you don�t need to add a context into the Tomcat server. Justdeploy the service into SOAP using the following deployment descriptorfile:

<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 won�t explain here how to set up Apache SOAP into Tomcat since there is enough guidance in the Apache SOAP documentation.

Apache SOAP Client
It�s 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 won�t work either.

.NET Service and Clients
.NETFramework Beta 2 is the newest technology and changes are expected tohappen from Beta 2 to the final release. Major changes already havebeen made when Beta 2 was released. This is no surprise since Microsofthad warned developers that might happen.

Writing a web service with .NET is very simpleand 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;
}
}

Theadvantage of using a ASMX file is that no compilation is needed so ahot deployment can be done.Place the file into a virtual directory under IIS. You can test theservice with your IE with http://localhost/testdotnetws/myservice.asmx.NET Client
Writing a client for this service is similarwith the .NET client we write earlier. When generating the proxy filespecify the WSDL file withhttp://localhost/testdotnetws/myservice.asmx?WSDL. This is the way .NETframework creates the WSDL file on-demand.

STK Client
Using the high level APIwould be a faster solution but there are some issues there and Icouldn�t solve them out so I used the low level API. The client code isnot difficult. The only trick is to enable the .NET service for RPCtype of calls. Thanks to Christian Weyer who helped me with this issue.Look at the web service code and notice theSystem.Web.Services.Protocols.SoapRpcMethodAttribute attribute for ourmethod. Without this attribute the default type of conversation in .NETis �message�.

Java Client
In the java client you�ll have to modify the url as follows:

<
p>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!

Related Articles :

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!