1.7. Consume a .NET web service with Java

1.7.1. Make a client with AXIS for the Infobel .NET (document/literal) web service.

I begin here, with Java, several demos about how to consume a .NET web service. For each language - and library - covered, you will find a SOAP client for a .NET web service. I will use the Infobel web service, because I know it very well ;-). And because it's a relatively advanced interface: taking object as param and returning objects containing collection of objects, containing... and so on. See this service on-line: Infobel web service. Go to the Section called Tip: a way to solve interoperability problem with a NuSoap client (for document/literal web service). in Chapter 2 for a description of this service. While constructing our client, we will see that if you have a tool like WSDL2Java, life can be so simple.

ImportantCaution
 

The data exposed by this web service are obsolete, are from a few years ago, and can only be used for demo and testing purpose.

Let's start to create our stub and wrapper classes with AXIS.

[your_prompt]$ java org.apache.axis.wsdl.WSDL2Java http://hal.kapitol.com/infobelservices/service1.asmx?WSDL -W

Copy the structure of directories created, under your working directory in your favorite IDE.

Example 1-15. Client code for the Infobel web service

package your_package;

import javax.xml.rpc.Stub;

public class Client {

  public static void main(String[] args) {
	
    // start with the Locator class
    com.infobel.www.WebService.InfobelSearchEnginesLocator isel =
      new com.infobel.www.WebService.InfobelSearchEnginesLocator();

    try{
      // InfobelSearchEnginesSoap port
      Stub st = (Stub)isel.getPort(com.infobel.www.WebService.InfobelSearchEnginesSoap.class);

      // Stub
      com.infobel.www.WebService.InfobelSearchEnginesSoapStub isess =
	(com.infobel.www.WebService.InfobelSearchEnginesSoapStub)st;
      
      // Where does CQuery comes from? Look in the wsdl and you will find
      // it is needed by inputQuery to create the Search request
      com.infobel.www.WebService.CQuery cq = new com.infobel.www.WebService.CQuery();

      // Use the _Search wrapper for the param to send
      com.infobel.www.WebService._Search request = new com.infobel.www.WebService._Search();

      // Define the fields for the CQuery object
      cq.setLogin("infobel");
      cq.setPassword("test");
      cq.setService(com.infobel.www.WebService.Services.aeSrvStandard);
      cq.setCountry(com.infobel.www.WebService.Countries.aeCountryBE);
      cq.setCity("bruxelles");
      cq.setName("dupont");
      cq.setLanguage(com.infobel.www.WebService.Lang.aeLangFrench);
      cq.setPageStep(5);
      cq.setRange(0);
      cq.setXCoord(0);
      cq.setYCoord(0);
      cq.setZip("");
      cq.setCoordType(com.infobel.www.WebService.CoordTypes.aeCTWGS);
			
      request.setInputQuery(cq);
	
      // Use the _SearchResponse wrapper to deserialize the complex type object
      // returned		
      com.infobel.www.WebService._SearchResponse resp = isess.search(request);
	
      // navigate in the structure of the objects and collections
      // A good IDE will help you to choose the right class and function
      com.infobel.www.WebService.CResponse cr = resp.getSearchResult();
			
      System.out.println("Status message: " + cr.getStatus());
	
      // In the CResult object you will find the number of 
      // result found + an object containing an array or record
      // CRecord[]		
      com.infobel.www.WebService.CResult cresult = cr.getResult();
			
      System.out.println("Record found: " + cresult.getNumRecs());
			
      com.infobel.www.WebService.ArrayOfCRecord aocr = cresult.getCollRecord();
			
      com.infobel.www.WebService.CRecord[] crarray = aocr.getCRecord();
	
      // In each record you will find eventually
      // an object containing a collection of category		
      for(int x = 0; x < crarray.length; x++) {
	System.out.println("Name: " + crarray[x].getName());
	System.out.println("Address: " + crarray[x].getAddress());
	System.out.println("City: " + crarray[x].getCity());
	System.out.println("Phone: " + crarray[x].getPhone());
	System.out.println("Zip: " + crarray[x].getZip());
	com.infobel.www.WebService.ArrayOfCCategory ccatColl = crarray[x].getCollCategory();
	if(ccatColl != null) {
	  com.infobel.www.WebService.CCategory[]  ccatar = ccatColl.getCCategory();
	  for(int y=0; y < ccatar.length; y++)
	    System.out.println(ccatar[y].getCode() + " " + ccatar[y].getDescription());
	}
	System.out.println();
      }

    }
    catch (Throwable t) {
      t.printStackTrace();
    }
  }
}