在我的Web应用程序中,我有一个全局静态HttpClient,它在应用程序的许多部分中使用。它是这样创建的:
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams params = new HttpConnectionManagerParams();
params.setConnectionTimeout( 15000 );
params.setSoTimeout( 15000 );
connectionManager.setParams(params);
httpclient = new HttpClient(connectionManager);
HttpClientParams clientParams = new HttpClientParams();
clientParams.setParameter("http.protocol.allow-circular-redirects", true);
clientParams.setParameter("http.protocol.max-redirects", 4);
httpclient.setParams(clientParams);
对于大多数使用来说,超时很好,但在特定的通话中,我希望更短的超时。所以我有:
GetMethod get = new GetMethod(finalUrl);
get.getParams().setParameter("http.socket.timeout", new Integer(1000));
get.getParams().setParameter("http.connection.timeout", new Integer(1000));
HttpClientUtil.getShortTimeoutInstance().executeMethod(get);
它不起作用。连接超时仍为15000.我可以在GetMethod中设置特定的连接超时而无需创建新的HttpClient实例(这是因为我相信创建一个新的HttpClient实例不是一个好主意。)
答案 0 :(得分:0)
你可能想看看
./ IMPL /康恩/ tsccm / ThreadSafeClientConnManager.java
方法
ClientConnectionRequest requestConnection(final HttpRoute route,
final Object state)
为路由初始化连接池后,将继续使用套接字timeOut的原始配置值,除非您扩展/覆盖它。
public ClientConnectionRequest requestConnection(
final HttpRoute route,
final Object state) {
final PoolEntryRequest poolRequest = pool.requestPoolEntry(
route, state);
return new ClientConnectionRequest() {
public void abortRequest() {
poolRequest.abortRequest();
}
public ManagedClientConnection getConnection(
long timeout, TimeUnit tunit) throws InterruptedException,
ConnectionPoolTimeoutException {
if (route == null) {
throw new IllegalArgumentException("Route may not be null.");
}
if (log.isDebugEnabled()) {
log.debug("Get connection: " + route + ", timeout = " + timeout);
}
BasicPoolEntry entry = poolRequest.getPoolEntry(timeout, tunit);
return new BasicPooledConnAdapter(ThreadSafeClientConnManager.this, entry);
}
};
}