在Java中使用需要自定义SOAP标头的.NET Web服务

时间:2009-01-19 18:03:16

标签: c# java web-services soap

所以我需要使用一个使用自定义SoapHeader的Web服务,如下所述。使用Java通过此标头传递正确值的最简单方法是什么。我正在使用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>
编辑:在Stack Overflow上显示XML的最佳方法是什么?

可能有助于添加Web服务在.NET中实现,我无法更改服务器端代码。

1 个答案:

答案 0 :(得分:3)

以下是基本步骤,假设您在客户端执行此操作:

  • 在服务界面上安装HandlerResolver(service.setHandlerResolver())
  • 覆盖HandlerResolver.getHandlerChain()以插入您自己的SOAPHandler实现
  • 实施SOAPHandler.handleMessage()以在发送之前修改SOAP标头

您可以通过请求上下文将参数传递给处理程序:

Map<String, Object> context = ((BindingProvider) port).getRequestContext();
context.put("userName', "foo");
...
在handleMessage()中你可以像这样得到标题:

public boolean handleMessage(SOAPMessageContext context) {
    ...

    SOAPMessage msg = context.getMessage();
    msg.getSoapHeader();
    ...

}

希望有所帮助。我猜也有办法用注释做这些东西。

相关问题