在Axis2中添加SOAP头

WSDL文件生成方式参照:

http://blog.csdn.net/a19881029/article/details/40194787

客户端代码生成方式参照:

http://blog.csdn.net/a19881029/article/details/40198649

最后生成的客户端代码格式如下:

MathStub.java类中的add方法有下面这一段代码:

[java]  view plain  copy
  1. ......  
  2. //其它构造函数最终都是调用这个函数创建MathStub对象  
  3. public MathStub(org.apache.axis2.context.ConfigurationContext configurationContext,  
  4.         java.lang.String targetEndpoint, boolean useSeparateListener)  
  5.         throws org.apache.axis2.AxisFault {  
  6.     populateAxisService();  
  7.     populateFaults();  
  8.     _serviceClient = new org.apache.axis2.client.ServiceClient(  
  9.             configurationContext, _service);  
  10.     _serviceClient.getOptions().setTo(  
  11.             new org.apache.axis2.addressing.EndpointReference(targetEndpoint));  
  12.     _serviceClient.getOptions().setUseSeparateListener(useSeparateListener);  
  13.     _serviceClient.getOptions().setSoapVersionURI(  
  14.             org.apache.axiom.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);  
  15. }  
  16. ......  
  17. public com.sean.AddResponse add(com.sean.Add add0)  
  18.         throws java.rmi.RemoteException{  
  19. ......  
  20.     // adding SOAP soap_headers  
  21.     _serviceClient.addHeadersToEnvelope(env);  
  22. ......  
  23. }  
  24. ......  

看了下ServiceClient的源码:

[java]  view plain  copy
  1. ......  
  2. private ArrayList<OMElement> headers;  
  3. ......  
  4. public void addHeader(OMElement header) {  
  5.     if (headers == null) {  
  6.         headers = new ArrayList<OMElement>();  
  7.     }  
  8.     headers.add(header);  
  9. }  
  10.   
  11. /** 
  12.  * Add SOAP Header to be sent with outgoing messages. 
  13.  * 
  14.  * @param header header to be sent (non-<code>null</code>) 
  15.  */  
  16. public void addHeader(SOAPHeaderBlock header) {  
  17.     if (headers == null) {  
  18.         headers = new ArrayList<OMElement>();  
  19.     }  
  20.     headers.add(header);  
  21. }  
  22.   
  23. /** Remove all headers for outgoing message. */  
  24. public void removeHeaders() {  
  25.     if (headers != null) {  
  26.         headers.clear();  
  27.     }  
  28. }  
  29.       
  30. public void addStringHeader(QName headerName, String headerText) throws AxisFault {  
  31.     if (headerName.getNamespaceURI() == null ||   
  32.             "".equals(headerName.getNamespaceURI())) {  
  33.         throw new AxisFault("Failed to add string header,"   
  34.                 + " you have to have namespaceURI for the QName");  
  35.     }  
  36.     OMElement omElement = OMAbstractFactory.getOMFactory()  
  37.             .createOMElement(headerName, null);  
  38.     omElement.setText(headerText);  
  39.     addHeader(omElement);  
  40. }  
  41.   
  42. /** 
  43.  * Add all configured headers to a SOAP envelope. 
  44.  * 
  45.  * @param envelope the SOAPEnvelope in which to write the headers 
  46.  */  
  47. public void addHeadersToEnvelope(SOAPEnvelope envelope) {  
  48.     if (headers != null) {  
  49.         SOAPHeader soapHeader = envelope.getHeader();  
  50.         for (Object header : headers) {  
  51.             soapHeader.addChild((OMElement)header);  
  52.         }  
  53.     }  
  54. }  
  55. ......  

修改方式已经很明确了,在MathStub类中创建私有方法,通过AXIOM(使用版本为:1.2.14)生成所需头对象,然后将该头对象添加至ServiceClient对象即可

[java]  view plain  copy
  1. ......  
  2. import org.apache.axiom.om.OMAbstractFactory;  
  3. import org.apache.axiom.om.OMElement;  
  4. import org.apache.axiom.om.OMFactory;  
  5. import org.apache.axiom.om.OMNamespace;  
  6. import org.apache.axis2.client.ServiceClient;  
  7. ......  
  8. public MathStub(org.apache.axis2.context.ConfigurationContext configurationContext,  
  9.         java.lang.String targetEndpoint, boolean useSeparateListener)  
  10.         throws org.apache.axis2.AxisFault {  
  11.     populateAxisService();  
  12.     populateFaults();  
  13.     _serviceClient = new org.apache.axis2.client.ServiceClient(  
  14.             configurationContext, _service);  
  15.     //添加SOAP头  
  16.     this.addHeaders(_serviceClient);  
  17.     _serviceClient.getOptions().setTo(  
  18.             new org.apache.axis2.addressing.EndpointReference(targetEndpoint));  
  19.     _serviceClient.getOptions().setUseSeparateListener(useSeparateListener);  
  20.     _serviceClient.getOptions().setSoapVersionURI(  
  21.             org.apache.axiom.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);  
  22. }  
  23. ......  
  24. private void addHeaders(org.apache.axis2.client.ServiceClient _serviceClient){  
  25.     OMFactory omFactory = OMAbstractFactory.getOMFactory();  
  26.     OMNamespace omNS=omFactory.createOMNamespace("http://sean.com","sean");  
  27.       
  28.     OMElement head = omFactory.createOMElement("Security", omNS);  
  29.     OMElement token = omFactory.createOMElement("Token", omNS);  
  30.     head.addChild(token);  
  31.           
  32.     OMElement userName = omFactory.createOMElement("Username", omNS);  
  33.     userName.addChild(omFactory.createOMText(userName, "root"));  
  34.     token.addChild(userName);  
  35.           
  36.     OMElement password = omFactory.createOMElement("Password", omNS);  
  37.     password.addAttribute(omFactory.createOMAttribute("Type"null"PasswordText"));  
  38.     password.addChild(omFactory.createOMText(password, "123"));  
  39.     token.addChild(password);  
  40.           
  41.     _serviceClient.addHeader(head);  
  42. }  
  43. ......  

添加SOAP头之前的请求格式如下:

[html]  view plain  copy
  1. <?xml version='1.0' encoding='utf-8'?>  
  2. <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">  
  3.     <soapenv:Body>  
  4.         <ns1:add xmlns:ns1="http://sean.com">  
  5.             <ns1:x>1</ns1:x>  
  6.             <ns1:y>1</ns1:y>  
  7.         </ns1:add>  
  8.     </soapenv:Body>  
  9. </soapenv:Envelope>  

添加SOAP头之后的请求格式如下:

[html]  view plain  copy
  1. <?xml version='1.0' encoding='utf-8'?>  
  2. <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">  
  3.     <soapenv:Header>  
  4.         <sean:Security xmlns:sean="http://sean.com">  
  5.             <sean:Token>  
  6.                 <sean:Username>root</sean:Username>  
  7.                 <sean:Password Type="PasswordText">123</sean:Password>  
  8.             </sean:Token>  
  9.         </sean:Security>  
  10.     </soapenv:Header>  
  11.     <soapenv:Body>  
  12.         <ns1:add xmlns:ns1="http://sean.com">  
  13.             <ns1:x>1</ns1:x>  
  14.             <ns1:y>1</ns1:y>  
  15.         </ns1:add>  
  16.     </soapenv:Body>  
  17. </soapenv:Envelope>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以通过以下步骤向SOAP添加用户名和密码: 1. 创建一个包含用户名和密码的SOAPHeader对象。 2. 将SOAPHeader对象添加SOAPEnvelope。 3. 将SOAPEnvelope对象设置为SOAPMessage对象的内容。 4. 使用SOAPConnection发送SOAPMessage对象。 以下代码片段演示了如何向SOAP添加用户名和密码: ```java // 创建SOAP消息 SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(); // 创建SOAP SOAPHeader soapHeader = soapMessage.getSOAPHeader(); // 创建身份验证信息 String username = "username"; String password = "password"; String authString = username + ":" + password; String authStringEnc = new String(Base64.encodeBase64(authString.getBytes())); // 添加身份验证信息到SOAP SOAPHeaderElement authHeader = soapHeader.addHeaderElement(new QName("http://example.com", "Authentication", "auth")); authHeader.addChildElement("Username").setValue(username); authHeader.addChildElement("Password").setValue(password); authHeader.addChildElement("AuthType").setValue("Basic"); authHeader.addChildElement("AuthData").setValue(authStringEnc); // 将SOAPEnvelope对象设置为SOAPMessage对象的内容 soapMessage.saveChanges(); // 发送SOAP消息 SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); SOAPMessage soapResponse = soapConnection.call(soapMessage, "http://example.com/soap"); ``` 请注意,上面的代码片段使用了Apache Commons Codec库的Base64类来编码用户名和密码。您需要将此库添加到您的项目,或者使用其他编码库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值