Java – Set custom SOAP header using Axis 1.4

axisjavanetsoapweb services

I'm trying to consume a .NET 2.0 web service using Axis.
I generated the web services client using Eclipse WST Plugin and it seems ok so far.

Here the expected SOAP header:

<soap:Header>
<Authentication xmlns="http://mc1.com.br/">
    <User>string</User>
    <Password>string</Password>
</Authentication>
</soap:Header>

I didn't find any documentation on how to configure this header from an Axis client.
When I generated the client using Visual Studio C# Express 2008, it generates a class named Authentication with two String attributes (User and Password) and all the client methods receive an object of this class as first parameter, but it did not happen with Axis WS client.

How can I set this header in my client calls?

Best Answer

Maybe you can use org.apache.axis.client.Stub.setHeader method? Something like this:

MyServiceLocator wsLocator = new MyServiceLocator();
MyServiceSoap ws = wsLocator.getMyServiceSoap(new URL("http://localhost/MyService.asmx"));

//add SOAP header for authentication
SOAPHeaderElement authentication = new SOAPHeaderElement("http://mc1.com.br/","Authentication");
SOAPHeaderElement user = new SOAPHeaderElement("http://mc1.com.br/","User", "string");
SOAPHeaderElement password = new SOAPHeaderElement("http://mc1.com.br/","Password", "string");
authentication.addChild(user);
authentication.addChild(password);
((Stub)ws).setHeader(authentication);

//now you can use ws to invoke web services...