2013-03-05 121 views
0

我已經使用soapUI和apache-cxf-2.7.2創建了https web服務的客戶端代碼。有一個名爲MYService_BasicEndpoint_Client.java包含此方法的類:如何在客戶端設置https Web服務的用戶名和密碼?

public static void main(String args[]) throws java.lang.Exception { 
    URL wsdlURL = MYServiceWcf.WSDL_LOCATION; 
    if (args.length > 0 && args[0] != null && !"".equals(args[0])) { 
     File wsdlFile = new File(args[0]); 
     try { 
      if (wsdlFile.exists()) { 
       wsdlURL = wsdlFile.toURI().toURL(); 
      } else { 
       wsdlURL = new URL(args[0]); 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
} 
    MYServiceWcf ss = new MYServiceWcf(wsdlURL, SERVICE_NAME); 
    IMYService port = ss.getBasicEndpoint(); 
    port.webserviceMethod(); 
} 

但是當我運行它的結果是HTTP響應401(未授權),因爲沒有任何選項來設置HTTPS網絡服務的用戶名和密碼。但是我可以在soapUI中運行此服務的測試用例,因爲有一個選項可以爲端點設置用戶名和密碼。我如何在由soapUI和apache-cxf-2.7.2創建的代碼中設置它們?

回答

0

你綁定到SOAPUI嗎?如果沒有,那麼我會推薦Jersey客戶端API。這裏有一個很好的例子,說明如何通過REST客戶端構建SOAP(http://aruld.info/soap-over-resty-client/

這使您可以使用Apache HTTPClient來設置您的請求(用戶名,密碼,端口和方案),然後創建Jersey WebResource最終請求分派

示例配置可能看起來像:

DefaultHttpClient httpClient = connector.getHTTPClient(argUsername, argPassword, 
        argScheme, argDomain); 

    ClientConfig config = new DefaultClientConfig(); 
    config.getClasses().add(SoapProvider.class); 
    HttpClientJerseyHandler clientHandler = new HttpClientJerseyHandler(httpClient, null, false); 
    HttpClientJerseyClient jerseyClient = new HttpClientJerseyClient(clientHandler, config); 
    return jerseyClient.resource(someWSDLURL);