14

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?

bluish
  • 26,356
  • 27
  • 122
  • 180
razenha
  • 7,660
  • 6
  • 37
  • 53

4 Answers4

34

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...
bluish
  • 26,356
  • 27
  • 122
  • 180
martsraits
  • 2,915
  • 3
  • 27
  • 25
  • after so many frustrating hours, this is the answer i needed. thanks – LiorH Dec 23 '10 at 16:19
  • 1
    `SOAPHeaderElement authentication = new SOAPHeaderElement("http://mc1.com.br/","Authentication");` This statement gives me and error `cannot initiate abstract class java` how to resolve this issue? – Salman Jun 08 '14 at 08:34
  • What if header is having 2 soapElement like this: – Saurabh Gadariya Jun 19 '17 at 22:25
3

If you have an object representing the Authentication container with userid and password, you can do it like so:

import org.apache.axis.client.Stub;

//...

MyAuthObj authObj = new MyAuthObj("userid","password");
((Stub) yourServiceObject).setHeader("urn://your/name/space/here", "partName", authObj);
bluish
  • 26,356
  • 27
  • 122
  • 180
Joe Zitzelberger
  • 4,238
  • 2
  • 28
  • 42
  • 1
    Which kind of object is `MyAuthObj`? Maybe the one that for the OP is `Authentication`? If so how can the client instantiate such an object?... Thanks! – bluish Nov 28 '12 at 16:29
1

I have the same issue and solved by the below fragement:

ServiceSoapStub clientStub = (ServiceSoapStub)new ServiceLocator().getServiceSoap(url);
org.apache.axis.message.SOAPHeaderElement header = new org.apache.axis.message.SOAPHeaderElement("http://www.abc.com/SSsample/","AuthHeader");
SOAPElement node = header.addChildElement("Username");
node.addTextNode("aat");
SOAPElement node2 = header.addChildElement("Password");
node2.addTextNode("sd6890");

((ServiceSoapStub) clientStub).setHeader(header);
bluish
  • 26,356
  • 27
  • 122
  • 180
ammu
  • 864
  • 2
  • 13
  • 27
1

I used almost the same solution as mentioned before:

SOAPHeaderElement cigWsHeader = new SOAPHeaderElement("https://ws.creditinfo.com", "CigWsHeader");
cigWsHeader.addChildElement("UserName").setValue("username");
cigWsHeader.addChildElement("Password").setValue("password");

var port = new ServiceLocator().getServiceSoap(someUrl);
((Stub) port).setHeader(cigWsHeader);

And spent hours searching why "Security header not found". And the aswer is... redundant symbol "/" at the namespace: "https://ws.creditinfo.com/" . Seriously! Keep it in mind.

Mike Menko
  • 819
  • 8
  • 9