This Piece of code can be modified based on the Webservice WSDL information and used as a Webservice Client. This Client sends a SOAP Request to the Webservice and receives back the response.
import org.apache.axis2.AxisFault;
import java.io.*;
import java.net.*;
import java.util.Iterator;
import java.util.Map;
import java.io.BufferedReader;
public class WSClient {
static final String url = "http://HOSTNAME:PORT/****/*****/..../*** "; //*SOAP ADDRESS LOCATION static final String urn = " ******* "; //*DFINITION NAME static String result = "";
public String getBaseMessage() {
String message = "<?xml version="1.0" encoding="utf-8"?>" +
"<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">" +
"<soapenv:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">" +
"</soapenv:Header>" +
"<soapenv:Body>" +
"<**INPUTMESSAGE PARENT TAG **xmlns="http://www.**.com/schemas/**Interface">"+
"<INPUT ELEMENT TAG 1>"+INPUT ELEMENT 1 +"</INPUT ELEMENT TAG 1>" + "<INPUT ELEMENT TAG 2>"+INPUT ELEMENT 2+"</INPUT ELEMENT TAG 2>" + "<INPUT ELEMENT TAG N>"+INPUT ELEMENT N+"</INPUT ELEMENT TAG N>" + "</****INPUTMESSAGE PARENT TAG***>" +
"</soapenv:Body>" +
"</soapenv:Envelope>";
return message;
}
public static void main(String[] args) throws ArrayIndexOutOfBoundsException
{
WSClient wsClient = new WSClient();
HttpURLConnection conn = null;
try
{
URL thisUrl = new URL (url);
conn = (HttpURLConnection)thisUrl.openConnection();
System.out.println("Opened connection");
}
catch(AxisFault ex)
{
ex.getFaultMessageContext();
}
catch(Exception ex)
{
ex.printStackTrace();
}
try {
String type = "application/soap+xml; charset=utf-8";
String soapAction = ""urn:" + urn + """;
conn.setRequestProperty("Content-Length",String.valueOf (wsClient.getBaseMessage().length()));
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setRequestProperty("SOAPAction", soapAction);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream out = conn.getOutputStream();
out.write(wsClient.getBaseMessage().getBytes("UTF-8"));
out.close();
System.out.println(conn.getResponseMessage());
System.out.println(conn.getResponseCode());
if(conn.getResponseMessage().equalsIgnoreCase("OK"))
wsClient.writeResult(conn.getInputStream());
else
wsClient.writeResult(conn.getErrorStream());
} catch(AxisFault ex)
{
ex.getFaultMessageContext();
}
catch(Exception e){
e.printStackTrace();
}catch (Throwable e)
{
e.printStackTrace();
}
finally {
conn.disconnect();
}
}
public void writeResult(InputStream stream) throws IOException {
StringBuffer sb = new StringBuffer("");
InputStreamReader isr = new InputStreamReader(stream, "UTF-8");
BufferedReader in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine);
System.out.println(inputLine);
}
in.close();
isr.close();
}
}