eclipse自带插件生成的webservice 客户端认证及soapenv:Header信息添加

最近对接一个soap webservice 接口,需要basic 认证和soap头标签认证:

1.eclipse生成webservice client代码。

2.找到XXXXXProxy.java 接口的代理类。

3.找到_initXXXXXProxy方法

 

//添加认证
javax.xml.rpc.Stub stub = (javax.xml.rpc.Stub)XXXXX;//stub 提取出来
stub._setProperty(javax.xml.rpc.Stub.USERNAME_PROPERTY, "username");//用户名
stub._setProperty(javax.xml.rpc.Stub.PASSWORD_PROPERTY, "password");//密码
//添加Header
/**
*     
*        ?
*        ?
*     
*  
*/
SOAPFactory soapFactory = SOAPFactory.newInstance();
SOAPElement wsSecHeaderElm = soapFactory.createElement("Authentification", AUTH_PREFIX, AUTH_NS);
SOAPElement userEle = soapFactory.createElement("user",AUTH_PREFIX, AUTH_NS);    
SOAPElement passwdElm = soapFactory.createElement("signature",AUTH_PREFIX, AUTH_NS);    
          
userEle.addTextNode("user");  
passwdElm.addTextNode("signature");
          
wsSecHeaderElm.addChildElement(userEle);
wsSecHeaderElm.addChildElement(passwdElm);
          
SOAPHeaderElement soapHeaderElement =  new SOAPHeaderElement(wsSecHeaderElm);
soapHeaderElement.setMustUnderstand(false);
stub.setHeader(soapHeaderElement);


if (_endpoint != null)
 stub._setProperty("javax.xml.rpc.service.endpoint.address", _endpoint);
else
      _endpoint = (String)(stub)._getProperty("javax.xml.rpc.service.endpoint.address");
4.这样就可以调用webservice的方法了
XXXXX poxy = new XXXXXProxy(URL);




你可能感兴趣的:(随记)