如何在 Apache http 客户端中设置连接超时?

发布于 2024-12-10 05:36:21 字数 1192 浏览 2 评论 0原文

我想使用 HTTPClient 运行线程安全的异步 HTTP 请求。我注意到它不尊重我的 CONNECTION_TIMEOUT 参数。

该代码是 ColdFusion / Java 的混合体。

client = loader.create("org.apache.http.impl.nio.client.DefaultHttpAsyncClient").init();
CoreConnectionPNames = loader.create("org.apache.http.params.CoreConnectionPNames");

client.getParams()
      .setIntParameter(JavaCast("string", CoreConnectionPNames.SO_TIMEOUT), 10)
      .setIntParameter(JavaCast("string", CoreConnectionPNames.CONNECTION_TIMEOUT), 10);

client.start();

request = loader.create("org.apache.http.client.methods.HttpGet").init("http://www.google.com");
future = client.execute(request, javacast("null", ""));

try {
   response = future.get();
}
catch(e any) {}

client.getConnectionManager().shutdown();

  无论我为 CONNECTION_TIMEOUT 提供什么,请求总是返回 200 OK。检查下面的输出。

  1. 如何设置有效的连接超时?
  2. CONNECTION_TIMEOUT 有什么作用吗?

输出

200 OK http://www.google.com/

200 OK http://www.google.com/

[snip]

5 requests using Async Client in: 2308 ms

I want to run thread safe, asynchronous HTTP requests using HTTPClient. I noticed that it does not respect my CONNECTION_TIMEOUT argument.

The code is ColdFusion / Java hybrid.

client = loader.create("org.apache.http.impl.nio.client.DefaultHttpAsyncClient").init();
CoreConnectionPNames = loader.create("org.apache.http.params.CoreConnectionPNames");

client.getParams()
      .setIntParameter(JavaCast("string", CoreConnectionPNames.SO_TIMEOUT), 10)
      .setIntParameter(JavaCast("string", CoreConnectionPNames.CONNECTION_TIMEOUT), 10);

client.start();

request = loader.create("org.apache.http.client.methods.HttpGet").init("http://www.google.com");
future = client.execute(request, javacast("null", ""));

try {
   response = future.get();
}
catch(e any) {}

client.getConnectionManager().shutdown();

Regardless of what I supply for CONNECTION_TIMEOUT, the requests always return 200 OK. Check the output below.

  1. How do I set an effective connection timeout?
  2. Does CONNECTION_TIMEOUT do anything?

Output

200 OK http://www.google.com/

200 OK http://www.google.com/

[snip]

5 requests using Async Client in: 2308 ms

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

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

发布评论

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

评论(2

最美的太阳 2024-12-17 05:36:21

apache 的 HttpClient 的文档有点参差不齐。在您的设置中尝试一下(它对我的版本 4 有效):

HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);

... set more parameters here if you want to ...

SchemeRegistry schemeRegistry = new SchemeRegistry();

.. do whatever you ant with the scheme registry here ...

ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);

client = new DefaultHttpClient(connectionManager, params);

The documentation for apache's HttpClient is kind of spotty. Try this in your setup (it worked for me with version 4):

HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);

... set more parameters here if you want to ...

SchemeRegistry schemeRegistry = new SchemeRegistry();

.. do whatever you ant with the scheme registry here ...

ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);

client = new DefaultHttpClient(connectionManager, params);
樱花细雨 2024-12-17 05:36:21

您必须使用框架的类方法定义 HttpParams 对象。

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
        HttpConnectionParams.setConnectionTimeout(params, 2000);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        HttpClient client = DefaultHttpClient(ccm, params);

You have to define a HttpParams object using the framework's class methods.

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
        HttpConnectionParams.setConnectionTimeout(params, 2000);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        HttpClient client = DefaultHttpClient(ccm, params);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文