0

I wrote the below code to send a SOAP request to an endpoint and get the response back. When I ran the code I am getting empty response. May I know what is wrong in my code. Thanks in advance. When I sent the same headers in SOAP UI , I am getting back the correct response. I feel some more logic needs to be added to it

        package com.siebel.Webservice;

    import java.io.IOException;
    import java.net.URL;

    import javax.xml.namespace.QName;
    import javax.xml.soap.MessageFactory;
    import javax.xml.soap.MimeHeaders;
    import javax.xml.soap.SOAPBody;
    import javax.xml.soap.SOAPBodyElement;
    import javax.xml.soap.SOAPConnection;
    import javax.xml.soap.SOAPConnectionFactory;
    import javax.xml.soap.SOAPElement;
    import javax.xml.soap.SOAPEnvelope;
    import javax.xml.soap.SOAPException;
    import javax.xml.soap.SOAPHeader;
    import javax.xml.soap.SOAPHeaderElement;
    import javax.xml.soap.SOAPMessage;
    import javax.xml.soap.SOAPPart;

    public class Main {

      public static void main(String[] args) throws Exception {
        SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
        SOAPConnection connection = sfc.createConnection();

        String endpoint = "https://mywebsite.org/eai_enu/start.swe?SWEExtSource=WebService&SWEExtCmd=Execute&WSSOAP=1";
        SOAPMessage response = connection.call(createSoapEnvelope(), endpoint);
        System.out.println(response.getContentDescription());
      }





      private static SOAPMessage createSoapEnvelope() throws SOAPException {
           MessageFactory messageFactory = MessageFactory.newInstance();
              SOAPMessage soapMessage = messageFactory.createMessage();

          SOAPPart soapPart = soapMessage.getSOAPPart();

          String myNamespace = "arm";
          String myNamespaceURI = "http://siebel.com/Webservice";

          // SOAP Envelope
          SOAPEnvelope envelope = soapPart.getEnvelope();
          envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);


          //SOAP Header
          SOAPHeader soapheader = envelope.getHeader();

          // Create a header block
          QName username = new QName("http://siebel.com/webservices", "UsernameToken");
          SOAPHeaderElement headerElement = soapheader.addHeaderElement(username);
          headerElement.addTextNode("username");


          QName password = new QName("http://siebel.com/webservices", "PasswordText");
          SOAPHeaderElement headerElement2 = soapheader.addHeaderElement(password);
          headerElement2.addTextNode("password");


          QName session = new QName("http://siebel.com/webservices", "SessionType");
          SOAPHeaderElement headerElement3 = soapheader.addHeaderElement(session);
          headerElement3.addTextNode("Stateless");




          // SOAP Body
          SOAPBody soapBody = envelope.getBody();
          SOAPElement soapBodyElem = soapBody.addChildElement("QueryList_Input", myNamespace);
          SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("SRNum", myNamespace);
          soapBodyElem1.addTextNode("");
          try {
              System.out.println("Soap message is ");
            soapMessage.writeTo(System.out);
            System.out.println();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          return soapMessage;
      }
    }
James
  • 337
  • 4
  • 12
  • 1
    It looks like your mistake is thinking that SOAPMessage.getContentDescription() is going to just return the SOAP envelope. Didn't `soapMessage.writeTo(System.out)` show it just fine? – Scott Kurz Oct 06 '17 at 16:13
  • When I wrote soapMessage.writeTO(Sytem.out) I get the below output SBL-EAI-04313IDS_EAI_WS_OP_NOT_FOUNDThere is no active Web Service with operation named 'http://siebel.com/Webservice:QueryList_Input'.(SBL-EAI-04313) – James Oct 06 '17 at 16:16

1 Answers1

1

You are probably using a wrong method. The one you call (getContentDescription()) will rather return content of "Content-Description" HTTP header.

Instead, to get a text representation of your SOAPMessage, you need to use some transformer, for example as described here: How to convert SOAPBody to String

user3714601
  • 1,156
  • 9
  • 27