SOAP Client in java

If you are looking for java client for RESTful web service then you should visit this article:

Simple REST client in java

This article will teach you how to create a SOAP client in java. That is creating a client in java which requests soap server (no need to be in java) and get response from it. First create request message as follows:

SOAPMessage message = MessageFactory.newInstance().createMessage();
SOAPHeader header = message.getSOAPHeader();
header.detachNode();

If you have namespace required in SOAP request then add those namespaces to envelope element as follows:

SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
envelope.setAttribute("namspace","namespaceUrl");

You can add as many attribute as you want. Now time to create request message body.
Following body is made assuming that SOAP server where this client will connect will have a public service method called getResponse(name) available.

SOAPBody body = message.getSOAPBody();
QName bodyName = new QName("getResponse");
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
SOAPElement symbol = bodyElement.addChildElement("name");
symbol.addTextNode(“Harry joy”);

Now that request message is ready it’s time to connect to soap server and send request. Following code will do this:

SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
SOAPMessage response = connection.call(message, endpoint);
connection.close();

In above code endpoint is the SOAP server URL without “?wsdl”. To parse response you can do as follows:

SOAPBody responseBody = response.getSOAPBody();
SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next();
SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next();
if(responseBody.getFault() != null) { //-- If response has any fault.
 	System.out.println(returnElement.getValue()+" "+responseBody.getFault().getFaultString());
}  else  {
   	System.out.println(returnElement.getValue());
}

Here request/response messages are totally dependent on SOAP server, how you have configured it.


Bonus: How to print request/response xml?

String getXmlFromSOAPMessage(SOAPMessage msg) throws SOAPException, IOException {
	ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
	msg.writeTo(byteArrayOS);
	return new String(byteArrayOS.toByteArray());
}

Use:

System.out.println(getXmlFromSOAPMessage(message));
System.out.println(getXmlFromSOAPMessage(response));

Full source code:

public class SOAPClient {

	private static final String endpoint = "http://localhost/SOAPService/MySoapService";

	public static void main(String[] args) throws SOAPException {
		SOAPMessage message = MessageFactory.newInstance().createMessage();
		SOAPHeader header = message.getSOAPHeader();
		header.detachNode();

		SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
		envelope.setAttribute("namespace","namespaceUrl");

		SOAPBody body = message.getSOAPBody();
		QName bodyName = new QName("getResponse");
		SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
		SOAPElement symbol = bodyElement.addChildElement("name");
		symbol.addTextNode("Harry Joy");

		SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
		SOAPMessage response = connection.call(message, endpoint);
		connection.close();

		SOAPBody responseBody = response.getSOAPBody();
		SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next();
		SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next();
		if(responseBody.getFault()!=null){
			System.out.println(returnElement.getValue()+" "+responseBody.getFault().getFaultString());
		} else {
			System.out.println(returnElement.getValue());
		}

		try {
			System.out.println(getXmlFromSOAPMessage(message));
			System.out.println(getXmlFromSOAPMessage(response));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static String getXmlFromSOAPMessage(SOAPMessage msg) throws SOAPException, IOException {
		ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
		msg.writeTo(byteArrayOS);
		return new String(byteArrayOS.toByteArray());
	}

}
Advertisement

18 thoughts on “SOAP Client in java

  1. Pingback: JavaPins
    1. Hi Marcelo,

      Sorry but I have never worked with NTLM, so can not make a statement here. But after a little search on internet I found that this is possible, you should try SOAP-UI.

      Thanks & Regards.

  2. I have a problem when connect with this code because the webservice return the message with conten-type application/xml and trigger a Exception “Invalid conten-type: application/xml. Is this an error mesage instead of a SOAP Response? ” You know how to resolve this??

    1. Hi Eduardo,

      I tried this code with a web service generating XML response and worked perfect. You should check the request XML you are sending is valid at server side. It can be possible that either you are not sending any content or too long contents or content you are sending in request is not in valid format for the server. If you want me to debug the issue then you have to send web service address and other associated details run this program on my personal email id, if its feasible for you. You can find my email id on about page. I’ll still try to see if I can reproduce this error and let you know the update. Also telling line on which you are getting this exception will be a good help to debug it.

      1. Hi Harry,

        I used your code to try to consume the following service:
        http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl

        The message that is being sent is the following:

        192.168.1.2

        whereas the message sent if I use SoapUI is the following:

        192.168.1.2

        apparently it looks to me like the problem is because IPAddress and GetGeoIP are not attributed to web ns. Am I right? If yes, how can I set them to follow the web ns?

        The error I’m getting is the following:
        Unable to handle request without a valid action parameter. Please supply a valid soap action.

        Thank you for any help!

        Jad

        1. N.B: this post contains the messages!

          Hi Harry,

          I used your code to try to consume the following service:
          http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl

          The message that is being sent is the following:

          <!–

          192.168.1.2

          –>

          whereas the message sent if I use SoapUI is the following:

          <!–

          192.168.1.2

          –>

          apparently it looks to me like the problem is because IPAddress and GetGeoIP are not attributed to web ns. Am I right? If yes, how can I set them to follow the web ns?

          The error I’m getting is the following:
          Unable to handle request without a valid action parameter. Please supply a valid soap action.

          Thank you for any help!

          Jad

          1. N.B: this time this post really contains the messages! You can delete the 2 other posts!

            Hi Harry,

            I used your code to try to consume the following service:
            http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl

            The message that is being sent is the following:

            <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" web="http://www.webservicex.net/">
              <SOAP-ENV:Body>
                <GetGeoIP>  
                  <IPAddress>192.168.1.2</IPAddress>
                </GetGeoIP>
              </SOAP-ENV:Body>
            </SOAP-ENV:Envelope>
             

            whereas the message sent if I use SoapUI is the following:

            
            <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webservicex.net/">
               <soapenv:Body>
                  <web:GetGeoIP>
                     <web:IPAddress>192.168.1.2</web:IPAddress>
                  </web:GetGeoIP>
               </soapenv:Body>
            </soapenv:Envelope>
             

            apparently it looks to me like the problem is because IPAddress and GetGeoIP are not attributed to web ns. Am I right? If yes, how can I set them to follow the web ns?

            The error I’m getting is the following:
            Unable to handle request without a valid action parameter. Please supply a valid soap action.

            Thank you for any help!

            Jad

            1. Finally the following code worked very well:

              package soapClientPackage;
              
              
              import javax.xml.soap.*;
              import javax.xml.transform.*;
              import javax.xml.transform.stream.*;
              
              public class SoapClient {
              
                  /**
                   * Starting point for the SAAJ - SOAP Client Testing
                   */
                  public static void main(String args[]) {
                      try {
                          // Create SOAP Connection
                          SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
                          SOAPConnection soapConnection = soapConnectionFactory.createConnection();
              
                          // Send SOAP Message to SOAP Server
                          String url = "http://www.webservicex.net/geoipservice.asmx";
                          SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
              
                          // Process the SOAP Response
                          printSOAPResponse(soapResponse);
              
                          soapConnection.close();
                      } catch (Exception e) {
                          System.err.println("Error occurred while sending SOAP Request to Server");
                          e.printStackTrace();
                      }
                  }
              
                  private static SOAPMessage createSOAPRequest() throws Exception {
                      MessageFactory messageFactory = MessageFactory.newInstance();
                      SOAPMessage soapMessage = messageFactory.createMessage();
                      SOAPPart soapPart = soapMessage.getSOAPPart();
              
                      String serverURI = "http://www.webservicex.net/";
              
                      // SOAP Envelope
                      SOAPEnvelope envelope = soapPart.getEnvelope();
                      envelope.addNamespaceDeclaration("web", serverURI);
              
                      /*
                      Constructed SOAP Request Message:
                      <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://www.webservicex.net/">
                          <SOAP-ENV:Header/>
                          <SOAP-ENV:Body>
                              <web:GetGeoIP>
                                  <web:IPAddressl>192.168.1.2</web:IPAddress>
                                  <web:LicenseKey>123</web:LicenseKey>
                              </web:GetGeoIP>
                          </SOAP-ENV:Body>
                      </SOAP-ENV:Envelope>
                       */
              
                      // SOAP Body
                      SOAPBody soapBody = envelope.getBody();
                      SOAPElement soapBodyElem = soapBody.addChildElement("GetGeoIP", "web");
                      SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("IPAddress", "web");
                      soapBodyElem1.addTextNode("192.168.1.2");
                      //example of adding another element    
                        //SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "web");
                        //soapBodyElem2.addTextNode("123");
              
                      
                      MimeHeaders headers = soapMessage.getMimeHeaders();
                      headers.addHeader("SOAPAction", serverURI  + "GetGeoIP");
              
                      soapMessage.saveChanges();
              
                      /* Print the request message */
                      System.out.print("Request SOAP Message = ");
                      soapMessage.writeTo(System.out);
                      System.out.println();
              
                      return soapMessage;
                  }
              
                  /**
                   * Method used to print the SOAP Response
                   */
                  private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
                      TransformerFactory transformerFactory = TransformerFactory.newInstance();
                      Transformer transformer = transformerFactory.newTransformer();
                      Source sourceContent = soapResponse.getSOAPPart().getContent();
                      System.out.print("\nResponse SOAP Message = ");
                      StreamResult result = new StreamResult(System.out);
                      transformer.transform(sourceContent, result);
                  }
              
              }
              
  3. hi I am getting [ Unable to handle request without a valid action parameter. Please supply a valid soap action. ] error when I use your code with endpoint = “http://www.webservicex.net/country.asmx?WSDL” , QName bodyName = new QName(“GetCurrencyByCountry”); , SOAPElement symbol = bodyElement.addChildElement(“CountryName”);
    symbol.addTextNode(“Singapore”); I want make a sample call to http://www.webservicex.net/country.asmx?op=GetCurrencyByCountry . Can you help me out?

    1. Hi kitokid,

      If you look at the link in your comment, they have displayed sample soap request as follows:

      <?xml version="1.0" encoding="utf-8"?>
      <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        <soap:Body>
          <GetCurrencyByCountry xmlns="http://www.webserviceX.NET">
            <CountryName>string</CountryName>
          </GetCurrencyByCountry>
        </soap:Body>
      </soap:Envelope>
      

      In this request they have used namespace uri for GetCurrencyByContry tag, which you are missing in your code. Add the namespace uri as follows and your code will work properly.

      QName bodyName = new QName("http://www.webserviceX.NET", "GetCurrencyByCountry");
      

      You can also add namespaces defined in envelope element as follows:

      SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
      envelope.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
      envelope.setAttribute("xmlns:xsd","http://www.w3.org/2001/XMLSchema");
      

      Hope this helps you.

      Have a good day !! 🙂

  4. Being a novice with Java, and required to call a Web Service with very limited time to implement a solution, your post has proven to be BY FAR the most helpful bit of information I have come across in the last 20 hours of research. Thank you!

  5. Hi I am new to soap,Igot
    “Unable to create message factory for SOAP: org.jboss.ws.core.soap.MessageFactoryImpl cannot be cast to javax.xml.soap.MessageFactory “—-
    if anyone knows plz tell me………..

  6. Hi I am new to soap,Igot
    “Unable to create message factory for SOAP: org.jboss.ws.core.soap.MessageFactoryImpl cannot be cast to javax.xml.soap.MessageFactory “—-
    if anyone knows plz tell me………..

  7. @harryjoy..
    Thanks a ton for this wonderful post. This has immensely helped me(for me being a novice).
    I have following questions. Could you please help me with this?
    1. If an EndPoint(A wsdl url) is given, Is there a way I can populate the available requests in Java? (This feature is available in SOAP UI. If you provide wsdl url, it populates requests)
    2. Can you please post your above post for this sample Web service available in this link. http://www.webservicemart.com/uszip.asmx?WSDL.

    I was not able to create the SOAPBody and Got stock there…

    Thank you again..
    MKrishna

Leave a Reply to M Krishna Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.