Return-Path: Delivered-To: apmail-hc-httpclient-users-archive@www.apache.org Received: (qmail 13773 invoked from network); 28 Jan 2009 18:23:10 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 28 Jan 2009 18:23:10 -0000 Received: (qmail 95831 invoked by uid 500); 28 Jan 2009 18:23:09 -0000 Delivered-To: apmail-hc-httpclient-users-archive@hc.apache.org Received: (qmail 95808 invoked by uid 500); 28 Jan 2009 18:23:08 -0000 Mailing-List: contact httpclient-users-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpClient User Discussion" Delivered-To: mailing list httpclient-users@hc.apache.org Received: (qmail 95797 invoked by uid 99); 28 Jan 2009 18:23:08 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Jan 2009 10:23:08 -0800 X-ASF-Spam-Status: No, hits=2.0 required=10.0 tests=FRT_LEVITRA,SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (nike.apache.org: local policy) Received: from [217.150.250.44] (HELO ok2consulting.nine.ch) (217.150.250.44) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 28 Jan 2009 18:22:59 +0000 Received: by ok2consulting.nine.ch (Postfix, from userid 1002) id AD15A198566; Wed, 28 Jan 2009 19:22:30 +0100 (CET) Received: from [192.168.1.102] (84-75-111-165.dclient.hispeed.ch [84.75.111.165]) by ok2consulting.nine.ch (Postfix) with ESMTP id 1E3D419855D for ; Wed, 28 Jan 2009 19:22:27 +0100 (CET) Message-ID: <4980A25D.7070306@apache.org> Date: Wed, 28 Jan 2009 19:22:21 +0100 From: Oleg Kalnichevski User-Agent: Thunderbird 2.0.0.19 (Windows/20081209) MIME-Version: 1.0 To: HttpClient User Discussion Subject: Re: TRANSLATING CODE FROM HTTPCLIENT3 TO HTTPCLIENT4 References: <20090128161812.C04CA81601E@nike.apache.org> In-Reply-To: <20090128161812.C04CA81601E@nike.apache.org> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on ok2consulting.nine.ch X-Spam-Level: X-Virus-Checked: Checked by ClamAV on apache.org X-Old-Spam-Status: No, score=-2.0 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.0.3 Joan Balaguer� wrote: > Hello, > > > > I�m migrating code from httpclient3 to httpcllient4, and there are some > things I do with httpclient3 that I�m not sure how to do with httpclient4. I > think my problems are in how to enable/disable cookies at connection manager > level, and how to manage idle connections (IdleConnectionHanfdler) at > connection manager level too. > Hi Joan (1) You really do not have to do anything special anymore. You might want to run a watcher thread that cleans up expired connections once in a while but you no longer need to manage idle connections manually. --- final ThreadSafeClientConnManager connman = new ThreadSafeClientConnManager( objHttpParams, schemeRegistry); class MyThread extends Thread { private volatile boolean shutdown = false; public void run() { while (!shutdown) { try { // Sleep for a minute this.wait(60000L); } catch (InterruptedException e) { break; } connman.closeExpiredConnections(); // optionally close connections that have been idle // over 5 min connman.closeIdleConnections(300, TimeUnit.SECONDS); } } public void shutdown() { shutdown = true; this.notifyAll(); } }; MyThread t = new MyThread(); t.start(); // Shutdown at the same time with the connection manager. t.shutdown(); connman.shutdown(); --- (2) You also ought not mess around with cookie policies anymore. HttpClient employs a new cookie policy per default that automatically picks up a cookie spec that matches best the capabilities of the target server. If, nonetheless, you want to enforce a particular cookie spec, you can use the following parameter either at the client or request level. --- DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter( ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); --- Hope this helps Oleg > > > > > ---- The code for httpclient3 is: > ---------------------------------------------------------------------------- > ---------------------- > > > > this.objHttp = new HttpClient(new MultiThreadedHttpConnectionManager()); > > this.objHttp.getHttpConnectionManager().getParams().setMaxTotalConnections(m > axConnections); // maxConnections > > this.objHttp.getHttpConnectionManager().getParams().setConnectionTimeout(con > nectionTimeout); // connectionTimeout > > this.objHttp.getParams().setSoTimeout(responseTimeout); // > responseTimeout > > > > // Manage �idle� connections. > > if (idleTimeout > 0) > > { > > this.ictt = new IdleConnectionsHandler(); > > this.ictt.addConnectionManager(this.objHttp.getHttpConnectionManager()); > > this.ictt.setTimeoutInterval(idleInterval); > > this.ictt.setConnectionTimeout(idleTimeout); > > this.ictt.start(); > > } > > > > // Timeout for taking connections from pool (1 ms --> we don�t wait) > > this.objHttp.getParams().setConnectionManagerTimeout(1); > > > > // "stale connections" and enable/disable "cookies". > > this.objHttp.getHttpConnectionManager().getParams().setStaleCheckingEnabled( > true); > > this.objHttp.getParams().setCookiePolicy(useCookies ? CookiePolicy.RFC_2109 > : CookiePolicy.IGNORE_COOKIES); > > > > > > ---- Now the code for httpclient4: > ---------------------------------------------------------------------------- > ---------------------- > > > > this.objHttpParams = new BasicHttpParams(); > > HttpProtocolParams.setVersion(this.objHttpParams, HttpVersion.HTTP_1_1); > > > > SchemeRegistry schemeRegistry = new SchemeRegistry(); > > schemeRegistry.register(new Scheme("http", > PlainSocketFactory.getSocketFactory(), 80)); > > schemeRegistry.register(new Scheme("https", > SSLSocketFactory.getSocketFactory(), 443)); > > > > ConnManagerParams.setMaxTotalConnections(this.objHttpParams, > maxConnections); // maxConnections > > HttpConnectionParams.setConnectionTimeout(this.objHttpParams, > connectionTimeout); // connectionTimeout > > HttpConnectionParams.setSoTimeout(this.objHttpParams, responseTimeout); > // responseTimeout > > > > // Manage �idle� connections. > > if (idleTimeout > 0) > > { > > NO IDEA. I could see �IdleConnectionHandler� and the method �add�, but at > connection level and not at connection manager level. > > } > > > > // Timeout for taking connections from pool (1 ms --> we don�t wait) > > ConnManagerParams.setTimeout(this.objHttpParams, 1); > > > > // "stale connections" and "cookies". > > HttpConnectionParams.setStaleCheckingEnabled(true); > > if (useCookies) new RFC2109SpecFactory().newInstance(this.objHttpParams); > <------ is this OK? And how to disable cookies?????? > > > > // And create the 'HttpClient' with the 'ThreadSafeClientConnManager'. > > ClientConnectionManager cm = new > ThreadSafeClientConnManager(this.objHttpParams, schemeRegistry); > > this.objHttp = new DefaultHttpClient(cm, this.objHttpParams); > > > > > > > > Thanks in advance, > > > > Joan. > > > > --------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org For additional commands, e-mail: httpclient-users-help@hc.apache.org