Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaJAX-WS添加SOAP头_Soap_Header_Jax Ws - Fatal编程技术网

如何使用JavaJAX-WS添加SOAP头

如何使用JavaJAX-WS添加SOAP头,soap,header,jax-ws,Soap,Header,Jax Ws,使用JAX-WS的典型SOAP客户端请求可能是 FooService service = new FooService(); FooPort port = service.getFooPort(); FooPayload payload = new FooPayload(); payload.setHatSize(3); payload.setAlias("The Hat"); ... port.processRequest(payload); 这将生成一个HTTP请求,内容如下 <?x

使用JAX-WS的典型SOAP客户端请求可能是

FooService service = new FooService();
FooPort port = service.getFooPort();
FooPayload payload = new FooPayload();
payload.setHatSize(3);
payload.setAlias("The Hat");
...
port.processRequest(payload);
这将生成一个HTTP请求,内容如下

<?xml ... ?>
<S:Envelope xmlns:S="http://...soap-envelope">
  <S:Body>
    <!-- payload -->
  </S:Body>
</S:Envelope>

通过操纵port.processRequest()调用的参数,您只能影响“负载”部分。不能影响XML消息的外部部分

我想在SOAP主体之前插入一个SOAP头

<S:Header>
   <X:Security xmlns:X="http://...wsssecurity...>
      <X:BinarySecurityToken>kjh...897=</X:BinarySecurityToken>
   </X:Security>
</S:Header>


你可能想看看处理程序和处理程序链。-我最近不得不向给定的Webservice调用添加一个cookie,我就是这样做的,只是创建了一个处理程序来截获初始调用并注入cookie,你还可以使用Pivot处理程序操纵调用头谢谢Nuno

只要我弄清楚如何正确登录stackoverflow.com,我就会用你的回复做正确的事情

同时,以下是我最终得到的代码:

FooService service = new FooService();
service.setHandlerResolver(new HandlerResolver() {
    public List<Handler> getHandlerChain(PortInfo portInfo) {
        List<Handler> handlerList = new ArrayList<Handler>();
        handlerList.add(new RGBSOAPHandler());
        return handlerList;
    }
});
FooPort port = service.getFooPort();
FooPayload payload = new FooPayload();
payload.setHatSize(3);
payload.setAlias("The Hat");
...
port.processRequest(payload);
FooService=newfooservice();
service.setHandlerResolver(新的HandlerResolver(){
公共列表getHandlerChain(PortInfo PortInfo){
List handlerList=new ArrayList();
add(新的RGBSOAPHandler());
返回handlerList;
}
});
FooPort-port=service.getFooPort();
FooPayload有效载荷=新的FooPayload();
有效载荷。设置大小(3);
有效载荷。setAlias(“帽子”);
...
处理请求(有效载荷);

类RGBSOAPHandler实现SOAPHandler{ 公共集getHeaders(){ 返回新树集(); } 公共布尔handleMessage(SOAPMessageContext上下文){ 布尔外边界属性= (布尔)context.get(MessageContext.MESSAGE\u出站\u属性); if(outboundProperty.booleanValue()){ SOAPMessage=context.getMessage(); 试一试{ SOAPEnvelope信封=context.getMessage() .getSOAPPart().getEnvelope(); SOAPFactory=SOAPFactory.newInstance(); 字符串前缀=“X”; 字符串uri=”http://...wsssecurity..."; SOAPElement安全元素= createElement(“安全性”,前缀,uri); SOAPElement标记元素= createElement(“BinarySecurityToken”,前缀,uri); tokenElem.addTextNode(“kjh…897=”); securityElem.addChildElement(tokenElem); SOAPHeader=envelope.addHeader(); header.addChildElement(securityElem); }捕获(例外e){ System.out.println(“处理程序中的异常:+e”); } }否则{ //入境 } 返回true; } 公共布尔handleFault(SOAPMessageContext上下文){ 抛出新的UnsupportedOperationException(“尚未支持”); } 公共无效关闭(MessageContext上下文){ // } }
对于添加Soap标头,如果您在web应用服务器上实现WS,Was将在标头处添加安全部分,在您按照WS-security标准进行配置后,例如web策略等。我不明白为什么需要添加您自己,除了加密的内容部分,例如加密密码等

WSDL是否描述了头?如果是这样,那么JAX-WS不生成代码来添加它们吗?我正在采取与您相同的步骤,但我得到以下异常:org.w3c.dom.DOMException:HIERARCHY\u REQUEST\u ERR:尝试在不允许的位置插入节点。你遇到过这个问题吗?该应用程序正在weblogic 10.1上运行
class RGBSOAPHandler implements SOAPHandler<SOAPMessageContext> {

    public Set<QName> getHeaders() {
        return new TreeSet();
    }

    public boolean handleMessage(SOAPMessageContext context) {
        Boolean outboundProperty = 
            (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if (outboundProperty.booleanValue()) {
            SOAPMessage message = context.getMessage();
            try {
                SOAPEnvelope envelope = context.getMessage()
                        .getSOAPPart().getEnvelope();
                SOAPFactory factory = SOAPFactory.newInstance();
                String prefix = "X";
                String uri = "http://...wsssecurity...";
                SOAPElement securityElem = 
                        factory.createElement("Security",prefix,uri);
                SOAPElement tokenElem = 
                        factory.createElement("BinarySecurityToken",prefix,uri);
                tokenElem.addTextNode("kjh...897=");
                securityElem.addChildElement(tokenElem);
                SOAPHeader header = envelope.addHeader();
                header.addChildElement(securityElem);

            } catch (Exception e) {
                System.out.println("Exception in handler: " + e);
            }
        } else {
            // inbound
        }
        return true;
    }

    public boolean handleFault(SOAPMessageContext context) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void close(MessageContext context) {
        //
    }
}