1

Does anyone know what I might be doing wrong here? I added the username and password to the tomcat-user.xml but I am still getting a "401 Unauthorized" error.

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();
Asaph
  • 159,146
  • 25
  • 197
  • 199
peekay
  • 1,259
  • 2
  • 25
  • 49

1 Answers1

1

The answer depends on whether you're using Tomcat 5.5/6 or Tomcat 7.

Tomcat 7

You'll need to access the the listing with the following URL:

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

You'll need to configure tomcat-user.xml with a user that has the manager-script role.

Tomcat 5.5/6

You'll need to access the the listing with the following URL:

http://localhost:9080/manager/list

You'll need to configure tomcat-user.xml with a user that has the manager role.

Update

You may not be performing the Basic Auth step correctly. The username/pass might need to be base64 encoded. See the highest voted answer in this question: Http Basic Authentication in Java using HttpClient?

Community
  • 1
  • 1
Asaph
  • 159,146
  • 25
  • 197
  • 199
  • here is the entry in my tomcat-user.xml I had already added this entry prior to testing and still received the above mentioned error. – peekay Apr 17 '12 at 16:47
  • @peekay The next thing to look at is how you're doing the Basic Auth using `HttpPost`. I think the username/pass must be base64 encoded. See the highest voted answer in the question I linked to at the bottom of my answer. – Asaph Apr 17 '12 at 16:51
  • well I have a different error now so I guess that's progress :) now I am getting a "400 Bad Request" so I guess that means it was the encoding. Thanks – peekay Apr 17 '12 at 17:21
  • @peekay: Make sure you use the format `user:pass` for the string that you base64 encode. – Asaph Apr 17 '12 at 17:29
  • @peekay Also, you should probably be using `HTTP GET` for this operation instead of `HTTP POST`. – Asaph Apr 17 '12 at 17:29
  • So as it turns out the 400 Bad request was due to the Base64 encoder adding a '\n' to the end of the end of the final string value. I removed the '\n' and resubmitted and the world was filled with hugs and puppy dogs. thanks everyone for all your help. – peekay Apr 25 '12 at 13:23
  • @peekay Thanks for the update. I was wondering how you finally resolved this. – Asaph Apr 25 '12 at 16:00
  • the first problem was because I forgot to add basic auth to the request. I was doing the base64 encryption but never added it to the request. the second problem (400 bad request) was due to a '\n' at the end of the base64 encoded string. for what ever reason the base64 encoder was adding the \n to the end of the sting that then caused the request to crash and burn. – peekay Apr 26 '12 at 20:05