2010-08-27 302 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