SOAP service call fails when SOAPMessage.writeTo is not called

I am trying to call a SOAP service , but after I build mine SOAPMessage

, if I SOAPMessage.writeTo(out)

call the service call succeeds, but when I omit it, it fails.

I'm pretty sure the call is writeTo()

not a required step before submitting the request, and I am doing something wrong.

Any ideas?

Here are the details

My client

public class Test {
    public static void main(String args[]) throws Exception {
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        // Send SOAP Message to SOAP Server
        String url = "https://mydomain.com/webservices/gateway/IdMgt/CorporateDirectoryLookupPort";
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

        // print SOAP Response
        soapResponse.writeTo(System.out);
        soapConnection.close();
    }

    private static SOAPMessage createSOAPRequest() throws Exception {

        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
                + "<soapenv:Header>"
                + "<ns1:Security soapenv:mustUnderstand=\"0\" xmlns:ns1=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
                + "<ns1:UsernameToken>"
                + "<ns1:Username></ns1:Username>"
                + "<ns1:Password></ns1:Password>"
                + "</ns1:UsernameToken>"
                + "</ns1:Security>"
                + "</soapenv:Header>"
                + "<soapenv:Body>"
                + "<GetAccountDetailsRequest2 xmlns=\"http://anotherdomain/schema/tCorporateDirectoryLookupV1\">"
                + "<MessageHeader xmlns=\"\"/><UserID xmlns=\"\"></UserID>"
                + "<AccountID xmlns=\"\">ServiceDeskAPIprd</AccountID>"
                + "</GetAccountDetailsRequest2></soapenv:Body>"
                + "</soapenv:Envelope>";

        InputStream is = new ByteArrayInputStream(xml.getBytes());
        SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(
                null, is);

        /* Print the request message */
        soapMessage.writeTo(System.out);
        System.out.println();
        return soapMessage;
    }
}

      

When it soapMessage.writeTo(System.out)

is createSOAPRequest

NOT commented out I get the correct answer, but when it is commented out I get

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
  <soapenv:Fault>
   <faultcode xmlns:ns1="http://xml.apache.org/axis/">ns1:Client.NoSOAPAction</faultcode>
   <faultstring>no SOAPAction header!</faultstring>
   <detail>
    <ns2:hostname xmlns:ns2="http://xml.apache.org/axis/">lxvirt150</ns2:hostname>
   </detail>
  </soapenv:Fault>
 </soapenv:Body>
</soapenv:Envelope>

      

+3


source to share


2 answers


While researching the implementation of the writeTo method, I found that they set the SOAPAction header at the end of it:

if (isCorrectSoapVersion(4)) {
        String[] soapAction = this.headers.getHeader("SOAPAction");

        if ((soapAction == null) || (soapAction.length == 0)) {
            this.headers.setHeader("SOAPAction", "\"\"");
    }
}

      

So, if you want to avoid calling the writeTo method, you can customize the header directly after the SOAPMessage is created:



SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(
            null, is);
soapMessage.getMimeHeaders().addHeader("SOAPAction", "\"\"");

      

This worked for me. Hope this helps!

Pierre

+1


source


I got the same problem but found saveChanges () method. From the java doc:



Updates this SOAPMessage object with all the changes that
have been made to it. This method is called automatically when
SOAPMessage writeTo(OutputStream) is called. However, if
changes are made to a message that was received or to one that has
already been sent, the method saveChanges needs to be
called explicitly in order to save the changes. The method saveChanges also generates any changes that can be read back (for example, a MessageId in profiles that support a message id). All MIME headers in a message that is created for sending purposes are guaranteed to have valid values only after saveChanges has been called.
In addition, this method marks the point at which the data from all
constituent AttachmentPart objects are pulled into the
message.

      

0


source







All Articles