1

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"?\>
<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/">
  <soap:Header>
    <CustomSoapHeader xmlns="http://snip">
      <UserName>"string"</UserName>
      <Password>"string"</Password>
    </CustomSoapHeader>
  </soap:Header>
  <soap:Body>
    <SomeWebMethod xmlns="http://snip" />
  </soap:Body>
</soap:Envelope>

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.

2
  • Did you try wrapping the XML in the <pre> and <code> tags?
    – Adrien
    Jul 14, 2009 at 22:57
  • Doesn't seem to I just did <pre> <code> <bunch of xml> </code> </pre> Is that right? Jul 15, 2009 at 10:32

1 Answer 1

3

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> 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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.