我们创建了一个派生自DefaultHttpClient
并使用ConnectionKeepAliveStrategy
. 现在我想知道在运行时是ClientConnectionManager
重用了一个连接还是创建了一个新的连接(例如,因为与服务器的连接超时)。
我怎么知道?
public class MyHttpClient extends DefaultHttpClient
{
// some code setting constants
public MyHttpClient()
{
super();
// some code setting http connection params
setKeepAliveStrategy(new ConnectionKeepAliveStrategy()
{
@Override
public long getKeepAliveDuration(final HttpResponse response, final HttpContext context)
{
return KEEP_ALIVE_TIMEOUT * MS_PER_SECOND;
}
});
}
@Override
protected ClientConnectionManager createClientConnectionManager()
{
// some code registering HTTP and HTTPS
return new ThreadSafeClientConnManager(getParams(), registry);
}
// some code for the socketfactory
}
// At some other class:
public class SomeClass
{
private static final HttpClient HTTP_CLIENT = new MyHttpClient();
public void someMethod()
{
/* some code */
HttpGet get = new HttpGet(some url);
HttpResponse getResponse = HTTP_CLIENT.execute(get);
/* more code */
}
}
当我们打电话时HTTP_CLIENT.execute()
,我如何才能看到新连接是由创建的ClientConnectionManager
还是现有的连接被重用?