Apache HttpClient超时

有没有一种方法来指定整个HttpClient执行超时?

我已经尝试了以下内容:

 httpClient.getParams().setParameter("http.socket.timeout", timeout * 1000); httpClient.getParams().setParameter("http.connection.timeout", timeout * 1000); httpClient.getParams().setParameter("http.connection-manager.timeout", new Long(timeout * 1000)); httpClient.getParams().setParameter("http.protocol.head-body-timeout", timeout * 1000); 

它实际上工作正常,除非远程主机发回数据 – 即使在一个字节/秒 – 它将继续永远阅读! 但是我想在最多10秒内中断连接,不pipe主机是否响应。

目前没有办法设置这种sorting的最大请求持续时间 :基本上你想说我不在乎是否有任何特定的请求阶段超时,但整个请求不能超过15秒 (例如) 。

最好的办法是运行一个单独的计时器,并在它到期时获取HttpClient实例使用的连接pipe理器,并closures连接,这应该终止连接。 让我知道这是否适合你。

对于较新版本的httpclient(例如http组件4.3 – https://hc.apache.org/httpcomponents-client-4.3.x/index.html ):

 int CONNECTION_TIMEOUT_MS = timeoutSeconds * 1000; // Timeout in millis. RequestConfig requestConfig = RequestConfig.custom() .setConnectionRequestTimeout(CONNECTION_TIMEOUT_MS) .setConnectTimeout(CONNECTION_TIMEOUT_MS) .setSocketTimeout(CONNECTION_TIMEOUT_MS) .build(); HttpPost httpPost = new HttpPost(URL); httpPost.setConfig(requestConfig); 

按照Femi的build议正常工作。 谢谢!

 Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { if(getMethod != null) { getMethod.abort(); } } }, timeout * 1000); 

build立其他的答案,我的解决scheme是使用HttpRequestInterceptor添加到每个请求中止可运行。 另外我换了ScheduledExecutorServiceTimer

 public class TimeoutInterceptor implements HttpRequestInterceptor { private int requestTimeout = 1 * DateTimeConstants.MILLIS_PER_MINUTE; private ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); public TimeoutInterceptor() { } public TimeoutInterceptor(final int requestTimeout) { this.requestTimeout = requestTimeout; } @Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { if (request instanceof AbstractExecutionAwareRequest) { final AbstractExecutionAwareRequest abortableRequest = (AbstractExecutionAwareRequest) request; setAbort(abortableRequest); } else if (request instanceof HttpRequestWrapper) { HttpRequestWrapper wrapper = (HttpRequestWrapper) request; this.process(wrapper.getOriginal(), context); } } /** * @param abortableRequest */ private void setAbort(final AbstractExecutionAwareRequest abortableRequest) { final SoftReference<AbstractExecutionAwareRequest> requestRef = new SoftReference<AbstractExecutionAwareRequest>(abortableRequest); executorService.schedule(new Runnable() { @Override public void run() { AbstractExecutionAwareRequest actual = requestRef.get(); if (actual != null && !actual.isAborted()) { actual.abort(); } } }, requestTimeout, TimeUnit.MILLISECONDS); } public void setRequestTimeout(final int requestTimeout) { this.requestTimeout = requestTimeout; } } 

在HttpClient 4.3版本,你可以使用下面的例子..让说5秒

 int timeout = 5; RequestConfig config = RequestConfig.custom() .setConnectTimeout(timeout * 1000) .setConnectionRequestTimeout(timeout * 1000) .setSocketTimeout(timeout * 1000).build(); CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build(); HttpGet request = new HttpGet("http://localhost:8080/service"); // GET Request response = client.execute(request); 

这也是工作:

 HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), timeout * 1000); HttpConnectionParams.setSoTimeout(client.getParams(), timeout * 1000);