我需要使用JAX-WS从响应中获取SOAP Header以使用WS。这是响应xml:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<sessaoIdHeader xmlns="http://www.cvm.gov.br/webservices/">
<Guid>9d63221b-8a3a-4fd6-84fe-100fa53a55d5</Guid>
<IdSessao>237196706</IdSessao>
</sessaoIdHeader>
</soap:Header>
<soap:Body>
<LoginResponse xmlns="http://www.cvm.gov.br/webservices/"/>
</soap:Body>
</soap:Envelope>
这是我的Java代码:
WsDownloadInfs infs = new WsDownloadInfs();
WsDownloadInfsSoap proxy = infs.getWsDownloadInfsSoap();
proxy.login(1779, "5270");
在这个操作中我没有返回(它是无效的)。
我尝试过像这样使用BindingProvider:
BindingProvider prov = (BindingProvider) proxy;
HeaderList list = (HeaderList) prov.getResponseContext().get("com.sun.xml.internal.ws.api.message.HeaderList");
但它没有任何属性。
答案 0 :(得分:2)
我找到了解决方案!
这就是我所做的:
我接受绑定提供程序,然后从以下位置获取响应HeaderList:
BindingProvider prov = (BindingProvider) proxy;
HeaderList list = (HeaderList) prov.getResponseContext().get("com.sun.xml.internal.ws.api.message.HeaderList");
所以,从HeaderList我得到我想要的标题(在这种情况下是“_SessaoIdHeader_QNAME”):
Header h =list.get(ObjectFactory._SessaoIdHeader_QNAME, true);
然后我创建一个SOAPMessage并将Header内容写入其中,以便从SOAPHeader中获取我想要的内容:
SOAPHeader header = message.getSOAPHeader();
NodeList idSessaoNode = header.getElementsByTagNameNS("*", "IdSessao");
String idSess = idSessaoNode.item(0).getChildNodes().item(0).getNodeValue();
String guid = header.getTextContent();
以下是完整的代码:
WsDownloadInfs infs = new WsDownloadInfs();
WsDownloadInfsSoap proxy = infs.getWsDownloadInfsSoap();
proxy.login(1779, "5270");
BindingProvider prov = (BindingProvider) proxy;
HeaderList list = (HeaderList) prov.getResponseContext().get("com.sun.xml.internal.ws.api.message.HeaderList");
Header h =list.get(ObjectFactory._SessaoIdHeader_QNAME, true);
SOAPMessage message = MessageFactory.newInstance().createMessage();
h.writeTo(message);
SOAPHeader header = message.getSOAPHeader();
NodeList idSessaoNode = header.getElementsByTagNameNS("*", "IdSessao");
String idSess = idSessaoNode.item(0).getChildNodes().item(0).getNodeValue();
String guid = header.getTextContent();
再次感谢!