Apache HttpComponents HttpClient 超时

发布于 2024-11-07 23:49:12 字数 338 浏览 0 评论 0原文

如何在httpcomponents httpclient中设置连接超时?我在以下位置找到了文档: http://hc.apache。 org/httpcomponents-client-ga/tutorial/html/connmgmt.html 但尚不清楚这些参数的实际设置方式。

此外,解释 SO_TIMEOUTCONNECTION_TIMEOUT 之间的差异也会有所帮助。

How do I set the connection timeout in httpcomponents httpclient? I have found the documentation at: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html but it is not clear how these parameters are actually set.

Also, an explanation of the difference between SO_TIMEOUT and CONNECTION_TIMEOUT would be helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

作死小能手 2024-11-14 23:49:12

在 Apache Http Client 4.3 版本中,配置被重构(再次)。新的方式看起来像这样:

RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder.setConnectTimeout(timeout);
requestBuilder.setConnectionRequestTimeout(timeout);

HttpClientBuilder builder = HttpClientBuilder.create();     
builder.setDefaultRequestConfig(requestBuilder.build());
HttpClient client = builder.build();

In version 4.3 of Apache Http Client the configuration was refactored (again). The new way looks like this:

RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder.setConnectTimeout(timeout);
requestBuilder.setConnectionRequestTimeout(timeout);

HttpClientBuilder builder = HttpClientBuilder.create();     
builder.setDefaultRequestConfig(requestBuilder.build());
HttpClient client = builder.build();
霞映澄塘 2024-11-14 23:49:12

在 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);

In HttpClient 4.3 version you can use below example.. let say for 5 seconds

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);
美人骨 2024-11-14 23:49:12

@jontro 的答案是正确的,但有一个关于如何执行此操作的代码片段总是很好。有两种方法可以执行此操作:

版本 1:为每个参数设置 10 秒超时:

HttpClient httpclient = new DefaultHttpClient();
// this one causes a timeout if a connection is established but there is 
// no response within 10 seconds
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10 * 1000);

// this one causes a timeout if no connection is established within 10 seconds
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10 * 1000);

// now do the execute:
HttpGet httpget = new HttpGet(URL);
HttpResponse response = httpclient.execute(httpget);

版本 2:还为以下每个参数设置 10 秒超时:

HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);
HttpConnectionParams.setSoTimeout(params, 10 * 1000);

HttpClient httpclient = new DefaultHttpClient(params);
HttpGet httpget = new HttpGet(URL);
HttpResponse response = httpclient.execute(httpget);

The answer from @jontro is correct, but it's always nice to have a code snippet on how to do this. There are two ways to do this:

Version 1: Set a 10 second timeout for each of these parameters:

HttpClient httpclient = new DefaultHttpClient();
// this one causes a timeout if a connection is established but there is 
// no response within 10 seconds
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10 * 1000);

// this one causes a timeout if no connection is established within 10 seconds
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10 * 1000);

// now do the execute:
HttpGet httpget = new HttpGet(URL);
HttpResponse response = httpclient.execute(httpget);

Version 2: Also set a 10 second timeout for each of these parameters:

HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);
HttpConnectionParams.setSoTimeout(params, 10 * 1000);

HttpClient httpclient = new DefaultHttpClient(params);
HttpGet httpget = new HttpGet(URL);
HttpResponse response = httpclient.execute(httpget);
放手` 2024-11-14 23:49:12

第 2.5 节中,您会看到一个示例,说明如何设置 CONNECTION_TIMEOUT 参数。

CONNECTION_TIMEOUT 是等待初始连接的时间,SO_TIMEOUT 是连接建立后读取数据包时等待的超时时间。

In section 2.5 you see an example of how to set the CONNECTION_TIMEOUT parameter.

CONNECTION_TIMEOUT is the time waiting for the initial connection and SO_TIMEOUT is the timeout that you wait for when reading a packet after the connection is established.

终陌 2024-11-14 23:49:12

我为整个请求设置了硬超时,以解决 java.net.SocketInputStream.socketRead0 问题。

private static final ScheduledExecutorService SCHEDULED_EXECUTOR = Executors.newSingleThreadScheduledExecutor()

HttpGet request = new HttpGet("http://www.example.com")
final Runnable delayedTask = new Runnable() {
    @Override
    public void run() {
        request.abort()
    }
}
SCHEDULED_EXECUTOR.schedule(delayedTask, 100000, TimeUnit.MILLISECONDS)

I set a hard timeout for the entire request to workaround the java.net.SocketInputStream.socketRead0 problem.

private static final ScheduledExecutorService SCHEDULED_EXECUTOR = Executors.newSingleThreadScheduledExecutor()

HttpGet request = new HttpGet("http://www.example.com")
final Runnable delayedTask = new Runnable() {
    @Override
    public void run() {
        request.abort()
    }
}
SCHEDULED_EXECUTOR.schedule(delayedTask, 100000, TimeUnit.MILLISECONDS)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文