简体   繁体   中英

consuming SOAP web service in java

I am looking for some alternatives of consuming a SOAP web service in java. I am currently using a stub method to consume it and it's too simple for my instructor needs. My instructor said to do a trivial client, what was that suppose to mean?

SOAP is basically the submission of XML to a web server using the POST method. While the XML can get verbose, you should be able to construct the XML using StringBuilder and then use a simple HTTP client, like the Apache HttpClient to construct a POST request to a URL using the XML string as the body.

That's about as simple as they come.

Here is the simple and lightweight example for consuming the soap api. Steps are below.

  1. You must create the SOAPTestController.java, KflConstants.java And SoapClient.java class.

  2. Then Implement the below code blocks and enjoy it.

Here is the SOAPTestController.java class

@Controller
public class SOAPTestController {

    @RequestMapping(value = "/showdate", method = RequestMethod.GET)
    public @ResponseBody String getDateAndTime() {

        String DateAndTimeSOAPRequest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"
                + "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\r\n"
                + "  <soap12:Body>\r\n" + "    <GetDateAndTime xmlns=\"http://tempuri.org/\" />\r\n"
                + "  </soap12:Body>\r\n" + "</soap12:Envelope>";
        String Fundtion = "GetDateAndTime";
        return new SoapClient().ConsumeTheService(DateAndTimeSOAPRequest, "GetDateAndTime");
    }
}

This is the 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 + "businesswebserviceNew/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";
}

Here is the SOAPClient .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();
        }
    }

Thanks,

SoapRequestBuilder s = new SoapRequestBuilder();
s.Server = "127.0.0.1"; // server ip address or name

s.MethodName = "ConcatWithSpace";
s.XmlNamespace = "http://tempuri.org/";
s.WebServicePath = "/SimpleService/Service1.asmx";
s.SoapAction = s.XmlNamespace+s.MethodName;
s.AddParameter("one", "David");
s.AddParameter("two", "Hobbs");
String response = s.sendRequest();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM