File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Services and the fly likes adding header to soap requests Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "adding header to soap requests" Watch "adding header to soap requests" New topic

adding header to soap requests

Alexandru Gifei
Ranch Hand

Joined: May 19, 2012
Posts: 40

I am developing a client side application in c# which is using non-.NET web services. I need to add a token to every request i make after i receive this token.
The header has to look like this:

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="">
<ws:AuthToken xmlns:ws="">

How can i add this token programatically in the soap header to every request i make?

Life is not measured by the number of breaths we take, but by the moments that take our breath away.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
You need to add a client-side webservice handler. You may want to read up on that.

So much trouble in the world -- Bob Marley
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41068
Normally you'd use a WS-Security library that comes with all major SOAP stacks, and as Bosun said, that's what client-side handlers do.

Ping & DNS - my free Android networking tools app
Alexandru Gifei
Ranch Hand

Joined: May 19, 2012
Posts: 40

I managed to resolve this issue by creating a new web service client based on soap protocol. Then i added the token to SoapClientMessage by overriding the GetWriterForMessage method.
Anand Bansal

Joined: Oct 15, 2012
Posts: 7
Hi All,

I am able to achieve this. But I want to add a custom header tag inside a SOAP Header. It should not have a SOAP URI and any namespaces.

<soapenv:Header xmlns:wsa="">
<wsse:Security xmlns:wsse=""><wsse:UsernameToken wsu:Id="UsernameToken-9419978" xmlns:wsu=""><wsse:Username>admin</wsse:Username><wsse:Password Type="">admin</wsse:Password></wsse:UsernameToken></wsse:Security>

<ABCHeader><MySubTag>textvalues</MySubTag></ABCHeader> ---------> This needs to be added.


Can someone help me out on this? I tried to add this through the below code and is adding extra information not required by me.

SOAPElement newHeader = soapheader.addChildElement("ABCHeader");
SOAPElement mySubTag = newHeader.addChildElement("MySubTag");

<soapenv:ABCHeader soapenv:actor="" soapenv:mustUnderstand="0"><soapenv:MySubTag>textValues</soapenv:MySubTag></soapenv:ABCHeader>

Expected Output:
subject: adding header to soap requests
Similar Threads
Axis client for jboss ejb3 session bean web service
SOAP header in SOAP request
sending pdf or any attachment from a java webservice to java client using JAX-WS and SAAJ
Customize AXISFault before reaching the Operation
AXIS2 : Inclusion of Headers into SOAP fault message and changing the namespaces