gpt4 book ai didi

java - HttpClient 上严格可靠的超时

转载 作者:搜寻专家 更新时间:2023-10-31 19:41:04 25 4
gpt4 key购买 nike

我正在使用 HttpClient 读取网页,如下所示:

        httpclient = new DefaultHttpClient();
httpget = new HttpGet("http://google.com");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream PIS = entity.getContent();
}

我需要在整个作业(连接、等待和读取 - 一起或单独)上暂停。
我尝试在 httpclient = new DefaultHttpClient(); 行之后设置超时参数:

        int timeout=10;
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倍左右就超时了)
所以我尝试了一个线程在 httpget = new HttpGet 之后使用 httpget.abort() & httpclient.getConnectionManager().shutdown() 一段时间后取消请求("http://google.com"); 像这样的行:

        (new Timer()).schedule(new java.util.TimerTask() {
public void run() {
httpget.abort();
httpclient.getConnectionManager().shutdown();
}
},10000);

但它没有效果(定时器运行;但这两行代码什么也没做!)!!
我也试过用这个:

URL url = new URL("http://google.com");
URLConnection con = url.openConnection();
con.setConnectTimeout(10000);
con.setReadTimeout(10000);
InputStream PIS = con.getInputStream();

但它和我第一次尝试一样(在HttpClient中设置超时参数)!!

问题是什么?
如何解决超时问题?

谢谢

最佳答案

不是解决方案,而是对正在发生的事情的更多解释。

你所做的是正确的。

首先,如果您使用的是 Log4J,请确保您看到 HttpClient 想要向您显示的所有内容:

log4j.logger.org.apache.http=trace

再看看这个类:

http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/conn/DefaultClientConnectionOperator.html

This connection operator is multihome network aware and will attempt to retry failed connects against all known IP addresses sequentially until the connect is successful or all known addresses fail to respond. Please note the same CoreConnectionPNames.CONNECTION_TIMEOUT value will be used for each connection attempt, so in the worst case the total elapsed time before timeout can be CONNECTION_TIMEOUT * n where n is the number of IP addresses of the given host.

这就是您的情况最有可能发生的情况。

此外,最好使用此接口(interface)中的常量 HttpConnectionParams :

SO_TIMEOUT = "http.socket.timeout"
TCP_NODELAY = "http.tcp.nodelay"
SOCKET_BUFFER_SIZE = "http.socket.buffer-size"
SO_LINGER = "http.socket.linger"
SO_REUSEADDR = "http.socket.reuseaddr"
CONNECTION_TIMEOUT = "http.connection.timeout"
STALE_CONNECTION_CHECK = "http.connection.stalecheck"
MAX_LINE_LENGTH = "http.connection.max-line-length"
MAX_HEADER_COUNT = "http.connection.max-header-count"
MIN_CHUNK_LIMIT = "http.connection.min-chunk-limit"

你只需要其中两个:

HttpConnectionParams.CONNECTION_TIMEOUT
HttpConnectionParams.SO_TIMEOUT

因此,解决此问题的最佳方法是实现一种仅返回一个 IP 地址的自定义 ClientConnectionOperator.resolveHostname 方法。

关于java - HttpClient 上严格可靠的超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9081342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com