The basic idea behind the XML Web services is to link one system to other independent of the platform. In this article we will see how we can make a simple java client that uses .net web service made in C#.

New Page 1

Introduction:

The basic idea behind the XML Web services is to link one system to other independent of the platform. In this article we will see how we can make a simple java client that uses .net web service made in C#.  

Tools Needed:

Trust me on this but you only need to write 3 lines of code to actually connect and use the Web Methods of the web service made in C#. But there are several tools that you must install in order to make the java client work with .net web service. Here is the list of tools that you will need.

1) JDK 1.4 (Download 1.4 since it is compatible with Axis).

2) Axis (A tool to communicate with the Web Service)

3) WSDLToJava (Tool to locate the Web Service)

4) Eclipse (Cool Java Editor)

Okay once you downloaded all the tools you need to install it.

 

Installation:

Install all the four tools on your hard drive. Once you have installed all 4 of them lets make some minor modifications. Go to Eclipse plug-in folder C:\Eclipse\eclipse\plugins (Path can be different if you have installed it in a different drive or directory) and copy com.myspotter.wsdl2java which can be found in the WSDLToJava folder. Okay now you are done with Installation lets now configure Eclipse so it can communicate with the .net web service.  

Configuring Eclipse: 

Run your eclipse application, it will take some time to load so go for shopping. Make a new workspace and after some time editor will open. Go to file select Java Application and than select new class from the File menu. Okay this was pretty much simple. Now come to the hard part. You need to add the jar files that are necessary in order to communicate with the .net web service. So let's add those jar files.

Right click on the project folder which will be the first node in the package explorer view on your right and select properties. A menu will appear select "java build path" and from the tab menu select "lib". Click on the Add External Jars and browse in your Axis folder until you find the lib folder. Select all the jar files in that folder and select open so that they can be added to your project.

Sample screenshot

 

Okay so now you have added the jar files that's cool. Now you need the wsdl file so that your java client will know about the web service. Make a small hello world Web service and run it in a browser and to get its wsdl file do something like this:

http://localhost/TimeService/TimeService.asmx?wsdl

Now you can see the wsdl file. View the source and copy the file anywhere in your hard disk. Now go back to your eclipse right click on the project folder select import -> File System -> Click Next. In the From directory give the path of your wsdl file. Since you have already install the WSDLToJava tool you will see a similar option which you right click on the wsdl file as you can see in the image below select Generate:

 Sample screenshot

Okay one last thing that you also need to check. Right click on the Project folder and select run it will open a menu as shown below:

 Sample screenshot

Make sure that the Main Class Text Field is the name of your class which contains the main method.

Okay now you are ready to make a small application so here is a simple hello world application that you can test. To test this application select run from the right click menu of the main method in the package Explorer.

 public class myClass { public static void main(String[] args) 
{ System.out.println("Hello World"); } }

Communicating with the web service:

Okay until now you have setup your tools and software so now you are ready to do your main work which is contacting .net Xml web service. Let's just see a simple web method of our .net web service written in C#. As expected the method returns simple Hello World. 

 [WebMethod] public string HelloWorld() { return "Hello World"; } 

Now lets come to the java code.

 // Creates Service Locator object Service1Locator locator = new Service1Locator(); 
// Creates the stub Service1SoapStub myStub = (Service1SoapStub) loc.getService1Soap();

One very important point to note is that you will start writing the code using the full namespace. It means the code which you see above is actually this:

 org.Tempuri.Service1Locator locator = new Service1Locator(); 
org.Tempuri.Service1SoapStub myStub = (org.tempuri.Service1SoapStub) loc.getService1Soap();

Once you have made the stub object which is the proxy of the .net web service you can access your "Hello World" method the same way as you did when you made the proxy in .net.

 System.out.println(stub.HelloWorld()); // This prints Hello World 

NOTE: 

So, you see that main problem was the installation and setting up the java client so that it works with .net service. After that it was pretty much the same. One important thing you must always remember is that if you make any changes in the Web Service, i.e. adding new web methods , editing old web methods. Always update the wsdl file as shown in fig 2 or else you won't be able to see the new/changed method.