gpt4 book ai didi

java - 如果不设置connectiontimeout、connectionmanagertimeout 和sockettimeout,httpclient 4.3 默认超时设置如何工作?

转载 作者:行者123 更新时间:2023-12-01 23:29:40 29 4
gpt4 key购买 nike

我最近使用了httpclient 4.3,我知道api已更改,但如果不设置超时阈值(conenction或socket或conenctionmanager),它可以工作,这意味着没有无限循环查询,method.getResponseBodyAsString()将返回一个空字符串,但是文档中说超时参数默认设置是无限的,那么它是如何工作的呢?

public class ContentModelUtils {
private static HttpClient client = new HttpClient();
...
public static String fetchPlainHttpResult(String id, Map<String, String> result, String getUrl)
throws HttpException, IOException {
method = new GetMethod(fetchPlainUrl(id, result, getUrl));
//client.getParams().setParameter("http.socket.timeout",1000);
//client.getParams().setParameter("http.connection.timeout",1000);
//client.getParams().setParameter("http.connection-manager.timeout",10000L);
client.executeMethod(method);
if (method.getStatusCode() != 200) {
return null;
}
String outputValue = new String(method.getResponseBodyAsString());

return outputValue;
}
...

最佳答案

默认设置实际上是无限超时。为了证明这一点,让我们浏览 source repository for Apache HttpCore 4.3.x .

BasicConnFactory ,我们可以看到它拉动了连接超时设置,并且检索超时参数的代码行使用默认值 0

this.connectTimeout = params.getIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 0);

稍后,在 BasicConnFactory#create ,这个超时值被传递到套接字连接中。

socket.connect(new InetSocketAddress(hostname, port), this.connectTimeout);

根据Socket#connect的文档,超时值 0(我们之前看到的是默认值)被解释为无限超时。

Connects this socket to the server with a specified timeout value. A timeout of zero is interpreted as an infinite timeout. The connection will then block until established or an error occurs.

关于java - 如果不设置connectiontimeout、connectionmanagertimeout 和sockettimeout,httpclient 4.3 默认超时设置如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34567599/

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