2013-01-15 56 views
0

我正在爲Restful Web服務編寫客戶端。用於JAVA的RESTful Web服務的NTLM解決方案

public static void main(String[] args){ 
     // Use apache commons-httpclient to create the request/response 
     HttpClient client = new HttpClient(); 
     Credentials defaultcreds = new UsernamePasswordCredentials("aaa", "cdefg"); 
     client.getState().setCredentials(AuthScope.ANY, defaultcreds); 


     GetMethod method = new GetMethod(
       "http://localhost:8080/userService/usersByID/1234"); 
     try { 
      client.executeMethod(method); 
      InputStream in = method.getResponseBodyAsStream(); 
      // Use dom4j to parse the response and print nicely to the output stream 
      BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 
      StringBuilder out = new StringBuilder(); 
      String line; 
      while ((line = reader.readLine()) != null) { 
       out.append(line); 
      } 
      System.out.println(out.toString()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

我得到未經授權的錯誤401。當我檢查服務器日誌。我發現了以下內容。

ERROR httpclient.HttpMethodDirector - Credentials cannot be used for NTLM authentication: org.apache.commons.httpclient.UsernamePasswordCredentials 

因此,我瞭解到我需要爲此使用NTLM身份驗證。

有人可以告訴我如何修改這個做NTLM身份驗證。

在此先感謝。

+1

http://hc.apache.org/httpcomponents-client-ga/ntlm.html –

回答

0

相反setCredentials方法的嘗試使用setProxyCredentials。

client.getState().setProxyCredentials(AuthScope.ANY, defaultcreds);