java WEBService 调用的 HTTP 基本认证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3588556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP Basic Authentication for WEBService call
提问by Java Guy
I trying to invoke a web service, which has an Basic HTTP Authentication. I generated the client code using the WSDL2JAVA tool from AXIS.
我试图调用具有基本 HTTP 身份验证的 Web 服务。我使用 AXIS 的 WSDL2JAVA 工具生成了客户端代码。
But I am not able to set the username and password to the webservice call.
但我无法为 webservice 调用设置用户名和密码。
I tried to have them in the endpoint url as
我试图将它们放在端点 url 中
But I am getting the unauthorized error for this. I am trying to figure out a way to get this set to my call in the Java code....
但是我收到了未经授权的错误。我试图找出一种方法来将这个设置到我在 Java 代码中的调用中....
Note : I am able to invoke the same service via the soapUI and get the results. I provided the username and password in the "Aut" tab on the request.
注意:我可以通过soapUI 调用相同的服务并获得结果。我在请求的“Aut”选项卡中提供了用户名和密码。
Here is some of the code snippets of my Stub, if this is userful for you
这是我的 Stub 的一些代码片段,如果这对你有用的话
_serviceClient = new org.apache.axis2.client.ServiceClient(configurationContext,_service);
_serviceClient.getOptions().setTo(new org.apache.axis2.addressing.EndpointReference(
targetEndpoint));
_serviceClient.getOptions().setUseSeparateListener(useSeparateListener);
//adding SOAP soap_headers
_serviceClient.addHeadersToEnvelope(env);
// set the message context with that soap envelope
_messageContext.setEnvelope(env);
// add the message contxt to the operation client
_operationClient.addMessageContext(_messageContext);
//execute the operation client
_operationClient.execute(true);
Any inputs will be greatly appreciated!!
任何输入将不胜感激!
回答by Aaron Saunders
HttpTransportProperties.Authenticator
auth = new HttpTransportProperties.Authenticator();
auth.setUsername("username");
auth.setPassword("password");
_serviceClient.getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.BASIC_AUTHENTICATE,auth);

