我想要运行使用HTTPClient线程安全的,异步HTTP请求。我注意到它不尊重我的CONNECTION_TIMEOUT
论点。如何在Apache HTTP客户端中设置连接超时?
的代码的ColdFusion/Java的混合体。无论什么样的我供应CONNECTION_TIMEOUT
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();
,请求总是返回200 OK。检查下面的输出。
- 如何设置一个有效的连接超时?
- 不CONNECTION_TIMEOUT做什么?
输出
200 OK http://www.google.com/
200 OK http://www.google.com/
[snip]
5 requests using Async Client in: 2308 ms
感谢。我正在尝试你的代码,但需要额外的工作才能将它转换为CF.和我一起裸露。在前两个linse中'HTTPConnectionParams'引用了我需要创建的一个对象。可能是'org.apache.http.params。*'但你提供给'setConnectionTimeout'的'params'参数来自哪里呢?我不能把它扔到那里,否则它会抛出一个'params is undefined'错误...对不起,如果这听起来很傻,但是使用java/CF混合代码是很乏味的。另外,SchemeRegistry对象是否必需,我可以将其忽略吗? – Mohamad
HttpParams params = new BasicHttpParams();这里有一些更多的信息:http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html和javadoc的是在这里:http://hc.apache.org/httpcomponents-core-ga /httpcore/apidocs/org/apache/http/params/HttpParams.html – Seth