Books I Like

Tuesday, October 13, 2009

How To : A Simple Generic SOAP Test Client written in Java

There are lots of libraries which are available to consume/generate a web service, like Spring Webservices, XFire, Apache Axis, to name a few.

While each one has its own pro/cons (that we can leave for a separate blog entry), many a times, one needs a simple (1 or 2) methods of a webservice calls to make. She may or may not care about the response, as long as the response is not leading to error.

For testing purposes, many use soapui, which is a wonderful tool. You may use it to point towards your soap end point, and it will generate request stub for you . Just fill in few params required by the method you are calling.

Here, I am ind of demonstrating the same thing, using a simple java program.
I am not using any WSDL2JAVA tool kind of stuff, to generate the stubs. [Please note, these stubs help you with the xml to java bindings, so if you are planning to use many methods, or you have sufficient time and want to do it right way, please follow that way].

Here is the code, you may use to call any end point. please note, it has some security headers just to show you. You may/may not need it.



package example;

import org.apache.axis.client.Service;
import org.apache.axis.client.Call;
import org.apache.axis.message.SOAPEnvelope;

import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.net.URL;


public class TestClient {
public static String wsURL =
"{YOUR_END_POINT}?WSDL";

public static String mySoapRequest =
new StringBuilder().
append("\n").
append(" \n").
#If your soap end point doesn't need security just remove these elements, or if it implements other sceurity standards, replace here
append(" \n").
append(" \n").
append(" \n").
append(" testusername\n").
append(" testpassword\n").
append("
\n").
append("
\n").
append("
\n").
append("
\n").
append(" \n").
#Here goes your request body
.......................
.......................
append("
\n").
append("
").toString();

public static void test() throws Exception {
InputStream input = new ByteArrayInputStream(mySoapRequest.getBytes());
Service service = new Service();
Call call = (Call) service.createCall();
SOAPEnvelope soapEnvelope = new SOAPEnvelope(input);
call.setTargetEndpointAddress(new URL(wsURL));
call.setUseSOAPAction(true);
System.out.println("Request:\n"+ mySoapRequest);
soapEnvelope = call.invoke(soapEnvelope);
System.out.println("Response:\n" + soapEnvelope.toString());
}

public static void main(String args[]) throws Exception {
TestClient callClient = new TestClient();
callClient.test();
}
}

3 comments:

Anonymous said...

I guess you may want to place a facebook icon to your blog. Just bookmarked this site, although I had to make this by hand. Simply my advice.

Djefferson W. Silva said...

the variable queryProfile, was not declared.

it must contain what?

Anshu said...

Instead of "queryProfile", it should be "mySoapRequest".
I edited the post, haven't this code with me now, so can't confirm, however that should work.

Get at Amazon