Consuming SOAP Web Service Using simple Application

controller.java class

/*
DateAndTimeSOAPRequest is SOAP Request
GetDateAndTime Is Soap API Name
*/
 new SoapClient().ConsumeTheService(DateAndTimeSOAPRequest, "GetDateAndTime");


SoalClient.java class
public class SoapClient {

 private static Logger log = LogManager.getLogger(SoapClient.class);

 /*Input Stream Convert to the String Object*/
 public static String convertStreamToString(java.io.InputStream is) {
  java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
  return s.hasNext() ? s.next() : "";
 }

 public String ConsumeTheService(String SOAPXML, String APINAME) {
  String Result = null;
  try {
   /*Create The Connection*/
   URL url = new URL(KflConstants.SERVICE_URL);
   URLConnection conn = url.openConnection();
   conn.setDoOutput(true);
   conn.setRequestProperty("Content-Type", 
                        KflConstants.CONTENT_TYPE_TEXT_XML);
   conn.setRequestProperty(APINAME, 
                        KflConstants.GET_DATE_AND_TIME_URL);
   log.info("Sending the envelope to server");
   /*Send the request XML*/
   OutputStream outputStream = conn.getOutputStream();
   outputStream.write(SOAPXML.getBytes());
   outputStream.close();
   /* Read the response XML*/
   log.info("Reading the Response");
   InputStream inputStream = conn.getInputStream();
   Result = convertStreamToString(inputStream);
   inputStream.close();
   /*INput Stream Convert to the SOAP Message*/
   InputStream is = new ByteArrayInputStream(Result.getBytes());
   SOAPMessage resposeSOAP = MessageFactory.newInstance()
                        .createMessage(null, is);
   /*Return Values*/
   log.info("Result SOAP:"+resposeSOAP.toString());
   log.info("Result String:"+Result);
   return Result;

  } catch (Exception e) {
   e.printStackTrace();
   log.error(e);
   return e.toString();
  }
 }

}

KflConstants.java Class

public class KflConstants {

 public static final String SERVER_IP = "http://192.168.0.222/";
 public static final String SERVICE_URL = SERVER_IP 
       + "bussinesswebserviceNew/service.asmx";
 public static final String CONTENT_TYPE_TEXT_XML = "text/xml; charset=utf-8";
 public static final String GET_DATE_AND_TIME_URL = SERVICE_URL + "/GetDateAndTime";

}


Post a Comment

Previous Post Next Post