Coding the client to interact with the Web service

Started by Kalyan, Nov 27, 2008, 11:01 AM

Previous topic - Next topic

Kalyan

Coding the client to interact with the Web service

With the Web service now running and a client project created we now have to create a new Java class in the client project to interact with the Web service. Do this by creating a new Java class within the Axis2 package that was generated; it should be something like "org.apache.ws.axis2". Note that this new Java class should have a main function as it will be run as a normal Java application when completed. Insert the following code:


package org.apache.ws.axis2;

import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;
import org.apache.ws.axis2.WSStub.*;

public class WSClient {

private final static String TARGET_EPR = "http://localhost:8080/Webservice/services/WS";
// CLIENT_TIMEOUT is in milliseconds. It indicates how long the client
// should wait for a response from the Web service before it times out (in milliseconds)
private final static int CLIENT_TIMEOUT = (10*60*1000);

public static void main(String[] args) {
String name = "James Ho";
String[] response = hashString(name);

if(response != null){

System.out.println("Returned name:"+response[0]);
System.out.println("Returned hash::"+response[1]);

}
}

public static String[] hashString(String name) {
String[] result = null;
try {
Return_hashResponse response;

WSStub stub = new WSStub(TARGET_EPR);
stub._getServiceClient().getOptions().setTimeOutInMilliSeconds(CLIENT_TIMEOUT);
Return_hash returnHash = new Return_hash();
returnHash.setName(name);
response = stub.return_hash(returnHash);

result = response.get_return();

} catch (AxisFault e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}

return result;
}
}


source : builderau