Ramson Mwangi
Ramson Mwangi

Reputation: 135

SAAJ: how to create child elements for SoapHeaderElement


Am trying to post some data to a remote machine, but it returns a fault

<soapenv:Fault>
   <faultcode xmlns:ns1="http://xml.apache.org/axis/">ns1:Client.NoSOAPAction</faultcode>
   <faultstring>no SOAPAction header!</faultstring>

My gues is that there is a problem in the way I am sending my header elements:

// Java code snip
SOAPHeader header = envelope.getHeader();
header.setPrefix("soapenv");
QName headerElementName = new QName("http://soft.com/webservices/", "AuthHeader");
SOAPHeaderElement authHeader = header.addHeaderElement(headerElementName);

QName headerChild = new QName("Username");
SOAPElement userName =  authHeader.addChildElement(headerChild);
userName.addTextNode("smart");

headerChild = new QName("Password");
SOAPElement passwd = authHeader.addChildElement(headerChild);
passwd.addTextNode("smart");

From the response, its clear that the soap toolkit on the remote machine is axis, and the final request sent and its response looks like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ocs="http://ocs.soft.com">
<soapenv:Header>
<AuthHeader xmlns="http://soft.com/webservices/">
<Username>smart</Username>
<Password>smart</Password>
</AuthHeader>
</soapenv:Header>
<soapenv:Body><ocs:doService><ocs:in0 xmlns:ocs="http://ocs.soft.com"><![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<zsmart>
   <Data>
    <header>
      <ACTION_ID>ModifyBalReturnAllBal</ACTION_ID>
      <REQUEST_ID>005200907310022</REQUEST_ID>
    </header>
    <body>
      <MSISDN>254775127966</MSISDN>
      <AccountCode></AccountCode>
      <BalID></BalID>
      <AddBalance>10000</AddBalance>
      <AddDays>0</AddDays>
      <TransactionSN>00520090731002195</TransactionSN>
    </body>
  </Data>
</zsmart>
]]></ocs:in0></ocs:doService></soapenv:Body></soapenv:Envelope>



HTTP/1.1 500 Internal Server Error
Content-Type: text/xml;charset=utf-8
Transfer-Encoding: chunked
Date: Wed, 20 Jan 2016 16:56:52 GMT
Server: Apache-Coyote/1.1
Connection: close

<?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/">ocstest</ns2:hostname>
   </detail>
  </soapenv:Fault>
 </soapenv:Body>
</soapenv:Envelope>

I've gone through the soap's documentation million times and am certain this is the valid soap request.
I want to know whether my header child elements part is well defined.
If so is it a problem of authentication on axis?

Upvotes: 0

Views: 2187

Answers (1)

Andreas Veithen
Andreas Veithen

Reputation: 9154

In this context, "header" doesn't refer to a SOAP header, but an HTTP header. With SAAJ, the SOAPAction header is set as follows:

MimeHeaders headers = message.getMimeHeaders();
headers.addHeader("SOAPAction", "http://www.example.org/someaction");

message is the SOAPMessage object. The SOAP action is specified in the WSDL of the service.

Upvotes: 1

Related Questions