webservice客户端添加soap Header信息

时间:2022-06-21 09:39:11

根据wsdl文件的header信息,在客户端中添加相应的header

1.wsdl信息如图

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cti="http://soap.sforce.com/schemas/class/CTIInteractionWebservice">
<soapenv:Header>
<cti:SessionHeader>
<cti:sessionId>123456</cti:sessionId>
</cti:SessionHeader>
</soapenv:Header>
<soapenv:Body>
</soapenv:Body>
</soapenv:Envelope>

2.java客户端代码

String namespace = "http://soap.sforce.com/schemas/class/Webservice"; //命名空间
String prefix = "cti"; //前缀
SOAPElement sessionHeaderElm = SOAPFactory.newInstance().createElement("SessionHeader",prefix,namespace); //SessionHeader标签
SOAPElement sessionIdElm = SOAPFactory.newInstance().createElement("sessionId",prefix,namespace); //sessionId标签
sessionIdElm.addTextNode("123456"); //内容
sessionHeaderElm.addChildElement(sessionIdElm); SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement(sessionHeaderElm);
binding.setHeader(soapHeaderElement);