Search code examples
javaweb-servicessoapsoapuisoap-client

SOAP Message as a String Sent to End Point in Java


I have a String which is a SOAP message.

String message = "<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header>
        <xyz:Config xmlns:hal="http://example.com/xyz" applicationId="Client" conversationId="000" host="ENDPOINT">
</xyz:Config>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <PaymentRQ xmlns="http://www.iata.org/IATA/4/0"
xmlns:common="http://www.iata.org/IATA/common/4/0">
            <PaymentDetails>
                  <PaymentDetail>
                        <common:PaymentCard CardNumber="1233444444444" CardType="100" ExpireDate="1120" SeriesCode="123">
                  </PaymentDetail>
            </PaymentDetails>
      </PaymentRQ>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
";

I need to send this as a SOAP request to a local server running on 8080 So, url would be http://localhost:8080/XYZService/xyz

Then fetch the SOAP response and read its values.

Kindly assist on how I can send the String as a SOAP message to the aforementioned url. Thanks in advance.


Solution

  • You can pass null to SOAPAction if you don't have one. for serverAddress pass serverIp + ServerPort(ex: 172...*:8088).

     public  String sendSoapRequest(String serverAdress, String message , String SOAPAction)
            {
                OutputStream httpOutputStream = null;
                byte[] byteArrayStream = message .getBytes();
    
                URL url = null;
                try {
                    url = new URL("http://"+serverAdress);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
    
                HttpURLConnection httpURLConnection = null;
                try {
                    httpURLConnection = (HttpURLConnection)url.openConnection();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                // Set the appropriate HTTP parameters.
                httpURLConnection.setRequestProperty("Content-Length",
                        String.valueOf(byteArrayStream.length));
                httpURLConnection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
                httpURLConnection.setRequestProperty("SOAPAction", SOAPAction);
                try {
                    httpURLConnection.setRequestMethod("POST");
                } catch (ProtocolException e) {
                    e.printStackTrace();
                }
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                try {
                    httpOutputStream = httpURLConnection.getOutputStream();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                //Write the content of the request to the outputstream of the HTTP Connection.
                try {
                    httpOutputStream.write(byteArrayStream);
                    httpOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
                BufferedReader httpInputBuufferedReader = null;
                try {
                    httpInputBuufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e(LOG_TAG,"IOException reading HTTP Input message ");
                    return null;
                }
    
                //Write the SOAP message response to a String.
                StringBuilder returnOutputString = new StringBuilder();
                try {
                    String line = "";
                    while ((line = httpInputBuufferedReader.readLine()) != null) {
                        returnOutputString.append(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e(LOG_TAG, "IOEception while reading HTTP input buffered reading");
                }
                return returnOutputString.toString();
            }