Return-Path: Delivered-To: apmail-jakarta-commons-httpclient-dev-archive@www.apache.org Received: (qmail 65942 invoked from network); 2 Oct 2004 18:17:13 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 2 Oct 2004 18:17:13 -0000 Received: (qmail 4247 invoked by uid 500); 2 Oct 2004 18:17:12 -0000 Delivered-To: apmail-jakarta-commons-httpclient-dev-archive@jakarta.apache.org Received: (qmail 4190 invoked by uid 500); 2 Oct 2004 18:17:11 -0000 Mailing-List: contact commons-httpclient-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Commons HttpClient Project" Reply-To: "Commons HttpClient Project" Delivered-To: mailing list commons-httpclient-dev@jakarta.apache.org Received: (qmail 4177 invoked by uid 99); 2 Oct 2004 18:17:11 -0000 X-ASF-Spam-Status: No, hits=0.1 required=10.0 tests=NORMAL_HTTP_TO_IP X-Spam-Check-By: apache.org Received-SPF: pass (hermes.apache.org: local policy) Received: from [194.94.224.6] (HELO mailserv.aei.mpg.de) (194.94.224.6) by apache.org (qpsmtpd/0.28) with ESMTP; Sat, 02 Oct 2004 11:17:11 -0700 Received: by mailserv.aei.mpg.de (Postfix, from userid 65534) id E53F827BDD0; Sat, 2 Oct 2004 20:17:08 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by mailserv.aei.mpg.de (Postfix) with ESMTP id 6873727C569 for ; Sat, 2 Oct 2004 20:17:08 +0200 (CEST) Received: from [127.0.0.1] (portal.aei-potsdam.mpg.de [194.94.224.212]) (using TLSv1 with cipher RC4-MD5 (128/128 bits)) (Client did not present a certificate) by mailserv.aei.mpg.de (Postfix) with ESMTP id E497B27C589 for ; Sat, 2 Oct 2004 20:16:57 +0200 (CEST) Message-ID: <415EF098.6090802@aei.mpg.de> Date: Sat, 02 Oct 2004 20:16:56 +0200 From: Jason Novotny User-Agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7.2) Gecko/20040803 X-Accept-Language: en, en-us MIME-Version: 1.0 To: commons-httpclient-dev@jakarta.apache.org Subject: convert usage if URLConnection to HttpClient Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS snapshot-20020531 X-Spam-Checker-Version: SpamAssassin 2.63 (2004-01-11) on mailserv.aei.mpg.de X-Spam-Level: X-Spam-Status: No, hits=-4.5 required=5.0 tests=AWL,BAYES_00, NORMAL_HTTP_TO_IP autolearn=no version=2.63 X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N 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: commons-httpclient-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-httpclient-dev-help@jakarta.apache.org