Access to SOAP Header from response payload

As I was trying to explain the difference between SOAPElement (a.k.a. XMLElement or Element in the java world) and Document, I got to realized that this know-how could be usefull when you need to access the content of the soap:header that is comming back from the service endpoint on the response.

To illustrate this technique, I'll be using WS-Addessing and the .NET endpoint Microsoft exposes for testing purposes (

In my test scenario, the request looks like this
<env:Envelope xmlns:env="" xmlns:xsd="" xmlns:xsi="">
<wsa:Action xmlns:wsa=""></wsa:Action>
<wsa:ReplyTo xmlns:wsa="">
<wsa:Address xmlns:wsa=""></wsa:Address>
<wsa:To xmlns:wsa=""></wsa:To>
<ans12:echoIn xmlns:ans12="">test1132</ans12:echoIn>

For now, I'll assume that you can figure out the way to send the proper SOAP Header in the request, and point you at the documentation if you need further information about JAX-RPC handlers.

To construct the client, you can use the following ant target:
<target name="genproxy">
 <delete dir="./gen_src"/>
  <o:genproxy wsdl="./etc/wcf-server/WSAddressingCR.wsdl" output="./gen_src" debug="true" classpath="./classes">
   <o:handler name="AddressingClientHandler" class="org.w3c.addressing.ClientHandler">
    <o:soapheader value="{}Action">
     <o:initparam name="test" value="testValue"/>

The trick to get access to the soap:header in the response is as follow:
  1. use the databinding="false" option on the o:genProxy command - this will change the method signature of your java code, and give you access to the raw XML.
  2. from the SOAPElement you are getting in return, get back to the containing XML Document - the soap:envelope. Once you have it, you can access any value from the soap:header
It's that simple. The draw back of this is on the java side - now, you have to construct the SOAP request and decode the SOAP response by hand. It's only a temporary solution, until we get to J2SE 6.0 and JAX-WS.

code snippet:
SOAPElement result = myPort.echo(input);
System.out.println("result as SOAPElement");
((XMLElement) result).print(System.out);
System.out.println("result as Document");
((XMLElement) result.getOwnerDocument().getDocumentElement()).print(System.out);


result as SOAPElement
<echoOut xmlns="">test1132</echoOut>
result as Document
<s:Envelope xmlns:s="" xmlns:a="">
<a:Action s:mustUnderstand="1"> </a:Action>
<echoOut xmlns="">test1132</echoOut>

In this example, the a:Action header is not that usefull, but imagine you can gain access to a:RelatesTo or a:MessageID using the same technique.

Have fun!!!


Popular posts from this blog

Changing the version of JDK used by JDeveloper

Connection reset from a WCF Web Service

Test locally first, then deploy to the cloud.