[英]NTLM solution for RESTful web services in JAVA
我正在為Restful Web服務編寫客戶端。
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身份驗證。
提前致謝。
請檢查以下鏈接
http://hc.apache.org/httpclient-3.x/authentication.html#NTLM http://davenport.sourceforge.net/ntlm.html
代替setCredentials嘗試使用setProxyCredentials。
client.getState().setProxyCredentials(AuthScope.ANY, defaultcreds);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.