Calling a remote SOAP WS (JAX-WS) from a local client

Lets say a some SOAP service is hosted in a remoter server. We dont want to know its implementation logic, language, platform, server or anything. Thats the beauty of WS. You can call it from any language, platform.

If you can google you can find loads of SOAP services for free. For now I will use http://www.webservicex.net/New/. This just a random choice. This site has loads of web services.

From all of those WS I will chose http://www.webservicex.net/New/Home/ServiceDetail/17. This can basically take a countries name as a string and provide its dialing code, currency, GMT like details.

So first step is to get it's WSDL.
Which is @ http://www.webservicex.net/country.asmx?WSDL

When you look at the end of the WSDL you can see this.


Which means 
Service name = country and its is binded  port = countrySoap

Before going to have a Java client we need to have all the stubs that complies with this WSDL.
we can do that via wsimport tool by JDK. Type this in a bash.

wsimport -keep http://www.webservicex.net/country.asmx?WSDL

-keep arg can keeps the .java files with .class files. What I normally do is after all the files parsed from the WSDL remove all the .class files via rm *.class 

So in our Java main method, we can have a instance of web service and get its binded interface which is at the above mentioned port and access all the web methods via the interface.


import net.webservicex.Country;
import net.webservicex.CountrySoap;

public class Main {

	public static void main(String[] args) {

		// According to WSDL service name is country
		// Create a web service instance
		Country country = new Country();
		
		// According to WSDL this service is binded to countrySoap port
		// So get this service's port
		CountrySoap cs = country.getCountrySoap();
		
		// Now we have the access to services interface
		// We can call its services 
		// We don't need to worry about in and out parameters and data types
		// They are being handled by stubs at client and server end
		// For now lets get the dialing number for any country
		String dialing = cs.getISD("Sri Lanka");
		
		System.out.println(dialing);
		
		
		
	}

}



Result

IMPORTANT
Actually this WS has 2 issues.
  1. It produces the result twice per each response.
  2. The result is not a native String type. It is wrapped around a table. we have to unwrap it first.

Any how Service implementation is beyond our duty and we can call the SOAP WS regardless.




underneath the communication media request is sent as a SOAP mag and response is also received as a SOAP msg.

We request

POST /country.asmx HTTP/1.1
Host: www.webservicex.net
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <GetISD xmlns="http://www.webserviceX.NET">
      <CountryName>string</CountryName>
    </GetISD>
  </soap12:Body>
</soap12:Envelope>
We get

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <GetISDResponse xmlns="http://www.webserviceX.NET">
      <GetISDResult>string</GetISDResult>
    </GetISDResponse>
  </soap12:Body>
</soap12:Envelope>


Share:

0 comments: