Tuesday, May 10, 2011

Simple WebService Client

You will need apache-axis.jar in your classpath. The following client is apache axis based. A similiar client can be built also using the JAX-WS only[without apache-axis.jar].



import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;

public class WEBSERVICE_CLIENT{
public static void main(String [] args) {
try {
String endpoint ="";

Service service = new Service();
Call call = (Call) service.createCall();

// YOU CAN SET VARIOUS PROPERTIES IN THE "CALL" OBJECT...LIKE
// USERNAME,PASSWORD,PROPERTIES OF HTTP HEADER. SEE "CALL" OBJECT'S API FOR MORE PROPERTIES.

call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName(new QName("", ""));

String msg = (String) call.invoke( new Object[] { "data" } );

System.out.println("RETURNED MESSAGE : "+msg);

} catch (Exception e) {
System.err.println(e.toString());
}
}
}