Jason,

Take a look at the HttpClient tutorial and the authentication guide.
That should get you started:

http://jakarta.apache.org/commons/httpclient/tutorial.html
http://jakarta.apache.org/commons/httpclient/authentication.html

Same code can be found here:

http://cvs.apache.org/viewcvs.cgi/jakarta-commons/httpclient/src/examples/BasicAuthenticationExample.java?rev=1.1.2.3&only_with_tag=HTTPCLIENT_2_0_BRANCH&view=markup

If all this will not help, let me know

Oleg

On Sat, 2004-10-02 at 20:16, Jason Novotny wrote:
> Hi,
> 
>     I had been using some crufty code using URLConnection to perform 
> basic auth to retrieve the list of applications using the Tomcat manager 
> webapp. I'd like to convert this to use commons-httpclient-2.0.1.
> 
>     It should be pretty simple I think-- what is required is to invoke 
> http://127.0.0.1/manager?list performing basic authentication using name 
> and password and then I get back a response which has a specific format.
>     My current code is shown below and I'd like to know what the 3 or 5 
> magic lines are to do the same thing using HttpClient.
> 
>     Thanks very much, Jason
> 
> try {
>             String serverName = req.getServerName();
>             int serverPort = req.getServerPort();
>             URL u = new URL("http://"; + serverName + ":" + serverPort + 
> "/manager" + command);
>             URLConnection con = u.openConnection();
> 
>             String up = USERNAME + ":" + PASSWORD;
>             String encoding = null;
>             // check to see if sun's Base64 encoder is available.
>             try {
>                 sun.misc.BASE64Encoder encoder =
>                         (sun.misc.BASE64Encoder)
>                         
> Class.forName("sun.misc.BASE64Encoder").newInstance();
>                 encoding = encoder.encode(up.getBytes());
>             } catch (Exception ex) { // sun's base64 encoder isn't available
>                 throw new TomcatManagerException("No 
> sun.misc.BASE64Encoder availoable in JDK!");
>             }
> 
>             con.setRequestProperty("Authorization", "Basic " + encoding);
>             con.setDoInput(true);
>             con.setUseCaches(false);
>             con.connect();
> 
>             if (con instanceof HttpURLConnection) {
>                 HttpURLConnection httpConnection = (HttpURLConnection) con;
>                 // test for 401 result (HTTP only)
>                 if (httpConnection.getResponseCode() == 
> HttpURLConnection.HTTP_UNAUTHORIZED) {
>                     throw new TomcatManagerException("HTTP Authorization 
> failure!");
>                 }
>             }
> 
>             BufferedReader reader = new BufferedReader(new 
> InputStreamReader(con.getInputStream()));
> 
>             // get first line
>             // should be something like:
>             // OK - some information text
>             String line = null;
> 
>             line = reader.readLine();
>             StringTokenizer tokenizer = new StringTokenizer(line, "-");
>             if (tokenizer.countTokens() == 2) {
>                 String rc = tokenizer.nextToken();
>                 String description = tokenizer.nextToken();
>                 result = new TomcatWebAppResult(rc, description);
>             }
> 
>             while ((line = reader.readLine()) != null) {
>                 result.addWebAppDescriptor(line);
>             }
>             reader.close();
> 
>         } catch (IOException e) {
>             throw new TomcatManagerException("Unable to perform command: 
> ", e);
>         }
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to