HttpClient 设置超时时间

2014-07-31· 7272 次浏览
## HttpClient 4.3.x: ```java RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(1000) .setConnectTimeout(1000) .build(); HttpGet httpget1 = new HttpGet("http://localhost/1"); httpget1.setConfig(requestConfig); ``` ## HttpClient 4: ### 连接超时: ```java httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,60000); // 或者HttpConnectionParams.setConnectionTimeout(params, 6000); ``` ### 读取超时: ```java httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,60000); // 或者HttpConnectionParams.setSoTimeout(params, 60000); ``` ## HttpClient 3: ### 连接超时: ```java httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(60000); ``` ### 读取超时: ```java httpClient.getHttpConnectionManager().getParams().setSoTimeout(60000); ```