Invoke Web Service using basic java.net package

Below code shows sample code to invoke Web Service Using basic java.net package.

InvokeService.java

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 *
 * @author Tejas Purohit
 */
public class InvokeService {

 public InvokeService() {

 }

 public static void main(String[] args) {

  // Setting Proxy Settings If Applicable, Consider below commented code
  // if you are working behind the proxy to access any web service.
  /*
  System.setProperty("proxySet", "true");
  System.setProperty("http.proxyHost", "192.168.1.100");
  System.setProperty("http.proxyPort", "8080");
  System.setProperty("http.proxyUser", "ProxyUser");
  System.setProperty("http.proxyPassword", "ProxyPassword");
   */

  // Web Service URL
  String url = "http://www.myapp.com/mywebservice/invoke";

  // SOAPAction
  String action = "http://www.myapp.com/mywebservice/action";

  // SOAPMessage
  String reqMessage = "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><Request><Emp><EmpNo>10</EmpNo><EmpName>Tejas Purohit</EmpName><EmpDesignation>Developer</EmpDesignation></Emp></Request></soap:Body></soap:Envelope>";

  InvokeService invokeService = new InvokeService();

  String res = invokeService.invoke(url, reqMessage, action);

  // Displaying Response
  System.out.println(res);

 }

 public String invoke(String strURL, String soapMessage, String soapAction) {

  String resMessage = null;

  try {

   // Initializing Connection with default properties
   URL url = new URL(strURL);
   HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
   urlCon.setAllowUserInteraction(true);
   urlCon.setDoOutput(true);
   urlCon.setDoInput(true);
   urlCon.setConnectTimeout(2000); // Setting Connection Timeout
   urlCon.setReadTimeout(2000); // Setting Read Timeout
   urlCon.setRequestProperty("Action", soapAction); // Setting SOAP Action
   urlCon.setRequestProperty("Content-Type", "text/xml"); // Setting Content Type
   urlCon.connect();

   // Openoutput stream and writting SOAPRequest Message
   OutputStream out = urlCon.getOutputStream();
   out.write(soapMessage.getBytes());
   out.flush();
   out.close();

   // Reading InputStream
   InputStream dataInputStream;
   if (urlCon.getResponseCode() == HttpURLConnection.HTTP_OK) {
    dataInputStream = urlCon.getInputStream();
   } else {
    dataInputStream = urlCon.getErrorStream();
   }
   resMessage = readInputStream(dataInputStream);
   dataInputStream.close();

   // Disconnecting
   urlCon.disconnect();

  } catch (Exception ex) {

   ex.printStackTrace();

  }

  return resMessage;

 }

 private String readInputStream(InputStream in) throws IOException {

  StringBuffer sb = new StringBuffer();

  int avail = in.available();

  while (avail > 0) {
   byte[] availBytes = new byte[avail];
   in.read(availBytes);
   sb.append(new String(availBytes));
   avail = in.available();
  }

  return sb.toString();

 }

}

Comments