gpt4 book ai didi

java - HttpClient 卡住了,没有任何异常

转载 作者:IT老高 更新时间:2023-10-28 20:27:44 29 4
gpt4 key购买 nike

我正在开发一个长时间运行的应用程序,它大量使用来自 apache 的 HttpClient。

在我的第一次测试运行中,该应用程序运行良好,直到它卡住了。它没有停止,也没有抛出任何异常,它只是坐在那里无所事事。

我刚刚进行了第二次运行,并停止了时间,大约在 1 分钟后停止了。 24小时不间断运行。此外,我注意到运行它的笔记本电脑的互联网连接在应用程序卡住的那一刻被终止。我必须重新启动我的 WLAN 适配器才能让网络再次运行。

但应用程序在连接再次建立后并没有恢复工作。而现在,又卡住了。

在 HttpClient 中是否有任何我不知道的超时 Controller ?为什么连接断开时我的应用程序不抛出异常?

使用客户端的部分如下所示;

public HttpUtil(ConfigUtil config) {
this.config = config;

client = new DefaultHttpClient();
client.getParams().setParameter(HttpProtocolParams.USER_AGENT, this.config.getProperty("httputil.userAgent"));
}

public String getContentAsString(String url) throws ParseException, ClientProtocolException, IOException {
return EntityUtils.toString(
client.execute(
new HttpGet(url)).getEntity());
}

应用程序在它需要的 URL 上反复调用 httputil.getContentAsString()

最佳答案

此代码现已弃用(获取 HttpParams 等)。更好的方法是:

RequestConfig defaultRequestConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.BEST_MATCH)
.setExpectContinueEnabled(true)
.setStaleConnectionCheckEnabled(true)
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
.build();

HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.build();
httpGet.setConfig(requestConfig);

关于java - HttpClient 卡住了,没有任何异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9925113/

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