2010-08-27 279 views
1

我试图调用具有基本HTTP认证的Web服务。我使用AXIS的WSDL2JAVA工具生成客户端代码。WEBService调用的HTTP基本认证

但我无法将用户名和密码设置为web服务调用。

我试图让他们在端点URL作为

http://username:[email protected]/wsdl

但我得到了未经授权的错误此。我试图找出一种方式来获得这一套我在Java代码中调用....

注:我能够调用通过soapUI的相同的服务,并获得满意的结果。我在请求的“Aut”选项卡中提供了用户名和密码。

下面是我的一些存根的代码片段,如果这是Userful公司为您

 _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); 

任何投入将不胜感激!

回答

5
HttpTransportProperties.Authenticator 
         auth = new HttpTransportProperties.Authenticator(); 
      auth.setUsername("username"); 
      auth.setPassword("password"); 

_serviceClient.getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.BASIC_AUTHENTICATE,auth); 
+2

谢谢!这工作。我发现只有这个虽然_serviceClient.getOptions()。setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE,auth); – 2010-08-30 16:04:37

+0

有人知道为什么证书无法放入问题中所示的端点URL吗?你可以用Apache HttpClient来做到这一点,我想Axis在幕后使用它...... – Kaitsu 2012-01-16 16:46:07