Title: Consuming in Java a .NET Web Service that requires a custom SOAP Header
Tags: c#;java;web-services;soap
Question: So I need to consume a Web Service that uses a custom SoapHeader, as described below. What is the simplest way to pass the correct values through this header using Java. I'm using Netbeans.
<?xml version="1.0" encoding="utf-8"?\&gt;
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
<soap:Header&gt;
<CustomSoapHeader xmlns="http://snip"&gt;
<UserName&gt;"string"</UserName&gt;
<Password&gt;"string"</Password&gt;
</CustomSoapHeader&gt;
</soap:Header&gt;
<soap:Body&gt;
<SomeWebMethod xmlns="http://snip" /&gt;
</soap:Body&gt;
</soap:Envelope&gt;
EDIT: What's the best way to display XML on Stack Overflow?
It might help to add that the Web Service is implemented in .NET and I cannot change the server side code.
Comment: Doesn't seem to
I just did
Is that right?

Comment: Did you try wrapping the XML in the  and  tags?

Here is the accepted answer: Here are the basic steps, assuming you're doing this on the client side:
Install a HandlerResolver on your service interface (service.setHandlerResolver())
Override HandlerResolver.getHandlerChain() to insert your own implementation of SOAPHandler
Implement SOAPHandler.handleMessage() to modify the SOAP header before it's sent out
You can pass parameters to your handler through the request context:
Map<String, Object&gt; context = ((BindingProvider) port).getRequestContext();
context.put("userName', "foo");
in handleMessage() you can get at the header like this:
public boolean handleMessage(SOAPMessageContext context) {
...
SOAPMessage msg = context.getMessage();
msg.getSoapHeader();
...
Hope that helps. I'm guessing there's also a way to do this stuff with annotations as well.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2009:00:01/19/09', 'FavoriteCount': 3.0, 'PostTypeId': 1, 'Tags': '<c#><java><web-services><soap>', 'Title': 'Consuming in Java a .NET Web Service that requires a custom SOAP Header', 'ViewCount': 3547, 'id': 458537, 'user_AboutMe': "<p>I'm a full stack Web Developer, working in CoffeeScript mainly. I'm pretty okay at other stuff, like HTML, CSS, SQL and NoSQL (Well, I've used MongoDB with my serious face on, and RethinkDB for fun). I used to be a .NET stack guy, so have a little residual knowledge of C#. I also really enjoy Regex, probably more than I should.</p>\n\n<p>In my 'spare' time, I'm a board gamer, reader (mainly of fantasy and sci-fi), PC gamer, and unofficial maths tutor to my step-daughter.</p>\n", 'user_AccountId': 10089.0, 'user_DisplayName': 'Mark Withers', 'user_Reputation': 1422.0, 'user_Views': 248.0, 'user_id': 18895.0}}