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(6L);
}
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: