Tomcat管理器通过HttpClient连接

时间:2012-04-17 16:28:16

标签: java tomcat basic-authentication

有谁知道我在这里做错了什么?我将用户名和密码添加到tomcat-user.xml但我仍然收到“401 Unauthorized”错误。

String url = "http://localhost:9080/manager/list";
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Authorization", "Basic " + userPass);

log.debug("executing request {}", httppost.getRequestLine());
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent()));
String line;
StringBuffer buffer = new StringBuffer();
while ((line = rd.readLine()) != null) {
buffer.append(line);
buffer.append('\n');
}
rd.close();

1 个答案:

答案 0 :(得分:1)

答案取决于您使用的是Tomcat 5.5 / 6还是Tomcat 7.

Tomcat 7

您需要使用以下网址访问该列表:

http://localhost:9080/manager/text/list

您需要使用具有manager-script角色的用户配置tomcat-user.xml。

Tomcat 5.5 / 6

您需要使用以下网址访问该列表:

http://localhost:9080/manager/list

您需要使用具有manager角色的用户配置tomcat-user.xml。

更新

您可能无法正确执行基本身份验证步骤。用户名/密码可能需要是base64编码的。请参阅此问题中投票最高的答案:Http Basic Authentication in Java using HttpClient?