0
訪問寧靜的Web服務時出現未授權錯誤。我的示例程序看起來像這樣。使用正確憑據訪問基於休息的Web服務時的響應代碼401
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();
}
}
我的憑據是正確的。我的Web服務將使用基本Http身份驗證。
我對認證範圍有疑問。
client.getState().setCredentials(AuthScope.ANY, defaultcreds);
我的憑據是正確的。
任何人都可以幫助解決這個問題。
謝謝。
感謝您的回覆。我可以通過瀏覽器訪問網址。我可以完美地打開它。 – Patan
分析瀏覽器的HTTP流量,你的java代碼找出差異 – TheWhiteRabbit