Soap web service client
Generic client for soap web service!. This client can be used for any soap web service. You have to pass only service endpoint "url" and soap request envelop!.
Here you go!
package com.vimal.activator;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
public class WebClient {
public static String executeWsdl(String wsdlUrl, String soapEnvelop) {
String outputString = "";
try {
soapEnvelop = URLDecoder.decode(soapEnvelop, "Utf-8");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[soapEnvelop.length()];
buffer = soapEnvelop.getBytes();
bout.write(buffer);
byte[] b = bout.toByteArray();
URL url = new URL(wsdlUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;
httpConn.setRequestProperty("Content-Length",
String.valueOf(b.length));
httpConn.setRequestProperty("Content-Type",
"text/xml; charset=utf-8");
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
out.write(b);
out.close();
// Response
InputStreamReader isr = new InputStreamReader(
httpConn.getInputStream());
BufferedReader rd = new BufferedReader(isr);
String line;
while ((line = rd.readLine()) != null)
outputString = outputString + line;
} catch (Exception e) {
e.printStackTrace();
}
return outputString;
}
private static String sampleRequestBody(String token, String serviceName) {
String xml = "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<Body>"
+ "<executeQuery xmlns=\"http://webservice.required.vimal.com\">"
+ "<filter xmlns=\"\"/>"
+ "<AuthToken xmlns=\"\">"
+ token
+ "</AuthToken>"
+ "<key xmlns=\"\">"
+ "<serviceName>"
+ serviceName
+ "</serviceName>"
+ "<dataType>JSON</dataType>"
+ "<Limit>[string?]</Limit>"
+ "<caseSensitive>true</caseSensitive>"
+ "</key>"
+ "</executeQuery>" + "</Body>" + "</Envelope>";
return xml;
}
public static void main(String[] args) throws Exception {
String xml = executeWsdl(
"http://localhost:8080/SoapWebService/Soap1",sampleRequestBody("token","servicename"));
System.out.println(xml);
}
}
I wonder, how many people still use Java for web service clients? I believe, that, nowadays, almost nobody uses Java. Will you agree with that?
ReplyDelete