WebService 401 unauthorized 授权认证错误
来源:互联网 发布:投诉淘宝卖家电话人工 编辑:程序博客网 时间:2024/09/21 09:02
使用Web Service时,有时服务器需要client客户端进行授权验证,这时需要在client端提供用户名和密码,否则抛出401 unauthorized未授权错误。对于这个问题,只要加上用户名、密码即可:
<span style="font-size:14px;">HttpTransportProperties.Authenticator basicauth = new HttpTransportProperties.Authenticator(); basicauth.setUsername("username"); //服务器访问用户名 basicauth.setPassword("password"); //服务器访问密码options.setProperty(HTTPConstants.AUTHENTICATE, basicauth);</span>
详细代码:
<span style="font-size:14px;">//Service的地址String serviceName = "http://192.168.0.96/sipxconfig/services/HelloService?wsdl";//命名空间,wsdl文件中definitions标签下的 targetNamespace的值String namespace = "http://www.sipfoundry.org/2007/08/21/ConfigService";//方法名,即要调用的方法,这里不详细解析,请参考:http://clq9761.iteye.com/blog/976029String methodName = "HelloService";Object[] methodArgs = new Object[] { };Class[] returnTypes = new Class[] { Object.class };RPCServiceClient serviceClient = new RPCServiceClient();Options options = serviceClient.getOptions();//设置密码HttpTransportProperties.Authenticator basicauth = new HttpTransportProperties.Authenticator(); basicauth.setUsername("username"); //服务器访问用户名 basicauth.setPassword("password"); //服务器访问密码options.setProperty(HTTPConstants.AUTHENTICATE, basicauth); EndpointReference targetEPR = new EndpointReference(serviceName);options.setTo(targetEPR); //options.setAction("urn:"+methodName);QName op= new QName(namespace,methodName);Object[] response = serviceClient.invokeBlocking(op, methodArgs,returnTypes);//返回结果String result = (String) response[0];</span>另外,如果是用wsdl2java.bat或eclipse插件生成代码的话,只需在生成相应的Stub类中的ConfigImplServiceStub的方法了添加下面几行代码即可:
<span style="font-size:14px;">HttpTransportProperties.Authenticator basicauth = new HttpTransportProperties.Authenticator(); basicauth.setUsername("username"); basicauth.setPassword("password");_serviceClient.getOptions().setProperty(HTTPConstants.AUTHENTICATE, basicauth);</span>
其中,_serviceClient为org.apache.axis2.client.ServiceClient的实例。以上的两种方法起始原理都一样的,属于NTLM认证,请参考:http://minjiaren.iteye.com/blog/432338
本文涉及到Axis2,详细配置和使用请参考:http://clq9761.iteye.com/blog/976029
博主寄语:写博客时,起一个长的博客名,是一种美德。尽量在博客名里写上与博文相关的字眼,这样可以增加读者搜索到博客的几率,在增加人气的同时也把你的智慧成果分享给他人。
尊重原著,转载请注明出处。
0 1
- WebService 401 unauthorized 授权认证错误
- HTTP 401 错误 - 未授权: (Unauthorized)
- java调用远程webservice服务报(401)Unauthorized错误
- 调用WebService时的401 Unauthorized问题
- 引用部署在IIS里的WebService时,解决“请求因 HTTP 状态 401 失败: Unauthorized。”的错误
- HTTP 401 Unauthorized错误--路由器页面获取
- WebService请求因 HTTP 状态 401 失败: Unauthorized。
- 解决手机unauthorized错误
- HTTP 错误 401.2 - Unauthorized
- 关于报urllib2.HTTPError: HTTP Error 401: Unauthorized错误问题
- Tomcat manager登录失败——401 Unauthorized错误
- Python模拟HTTPS请求返回HTTP 401 unauthorized错误
- Python模拟HTTPS请求返回HTTP 401 unauthorized错误
- 解决maven deploy时报401 ReasonPhrase: Unauthorized 错误
- webservice认证
- Tomcat 401 Unauthorized
- AAA认证、授权、计费
- FluorineFx:认证与授权
- 弟弟的作业
- LeetCode | Insertion Sort List
- Mysql多表查询外键
- 日趋“四低”的智能手环,出路在哪?
- Git的Patch功能
- WebService 401 unauthorized 授权认证错误
- 加密你的SQLite
- Win7开启Telnet服务,可以远程控制
- Android Layout XML属性 及 控件属性
- 经典c程序(0029) ---对称矩阵
- IIS 7.0、IIS 7.5 和 IIS 8.0 中的 HTTP 状态代码
- 会吓人的概念证明病毒: Chameleon
- pkg-config for mingw 安装及配置
- 最大递减数