axis2调用带Authentication认证的webservice

本文介绍如何通过配置HTTP基本认证解决Axis2客户端调用Web服务时遇到的用户名密码错误问题。通过设置用户名和密码,成功实现远程服务的调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java代码   收藏代码
  1. package com.wujianjun.axis2.client;  
  2.   
  3. import org.apache.axiom.om.OMAbstractFactory;  
  4. import org.apache.axiom.om.OMElement;  
  5. import org.apache.axiom.om.OMFactory;  
  6. import org.apache.axiom.om.OMNamespace;  
  7. import org.apache.axis2.AxisFault;  
  8. import org.apache.axis2.addressing.EndpointReference;  
  9. import org.apache.axis2.client.Options;  
  10. import org.apache.axis2.client.ServiceClient;  
  11. import org.apache.axis2.transport.http.HTTPConstants;  
  12. import org.apache.axis2.transport.http.HttpTransportProperties;  
  13.   
  14. public class TestClient {  
  15.     public static void main(String[] args) {  
  16.         try {  
  17.             ServiceClient sc = new ServiceClient();  
  18.             Options opts = sc.getOptions();  
  19.             opts.setTo(new EndpointReference("http://10.10.0.68:8000/sap/bc/srt
  20. /rfc/sap/ztest/110/ztest/ztest"));  
  21.             opts.setAction("urn:ZTEST");  
  22.             HttpTransportProperties.Authenticator basicAuth =
  23.  new HttpTransportProperties.Authenticator();  
  24.             basicAuth.setUsername("liuhand");  
  25.             basicAuth.setPassword("dev110");  
  26. //          basicAuthentication.setHost("xx.xx.x.xx");      
  27. //          basicAuthentication.setDomain("xx.xx.xx.xx");    
  28.             opts.setProperty(HTTPConstants.AUTHENTICATE, basicAuth);  
  29.             OMElement res = sc.sendReceive(createPayLoad());  
  30.             System.out.println(res);  
  31.         } catch (AxisFault e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.   
  35.     }  
  36.   
  37.     public static OMElement createPayLoad() {  
  38.         OMFactory fac = OMAbstractFactory.getOMFactory();  
  39.         OMNamespace omNs = fac.createOMNamespace("urn:sap-com:document:sap:rfc:functions","m");  
  40.         OMElement method = fac.createOMElement("ZTEST", omNs);  
  41.         OMElement value = fac.createOMElement("IM_P"null);  
  42.         value.setText("Hello");  
  43.         method.addChild(value);  
  44.         System.out.println(method);  
  45.         return method;  
  46.     }  
  47.   
  48. }  



如果不加
# HttpTransportProperties.Authenticator basicAuth = new HttpTransportProperties.Authenticator();  
#             basicAuth.setUsername("liuhand");  
#             basicAuth.setPassword("dev110");  
# //          basicAuthentication.setHost("xx.xx.x.xx");      
# //          basicAuthentication.setDomain("xx.xx.xx.xx");    
#             opts.setProperty(HTTPConstants.AUTHENTICATE, basicAuth);  
就会出现用户名密码错误的问题。
加上以上代码,把用户名和密码改成你项目中正确的用户名和密码就可以解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值