问题描述
限时送ChatGPT账号..当我尝试使用 HttpClient 连接时遇到问题到一个网址.即使在我设置之后,http 连接也需要更长的时间才能超时连接超时.
I'm running into an issue when i try to use the HttpClient connecting to a url. The http connection is taking a longer time to timeout, even after i set a connection timeoout.
int timeoutConnection = 5000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
它在大多数情况下都能完美运行.但是,每隔一段时间,http 连接就会永远运行并忽略 setconnectiontimeout
,尤其是当手机连接到 wifi 并且手机空闲时.
It works perfect most of the time. However, every once in while, the http connection runs for ever and ignore the setconnectiontimeout
, especailly when the phone is connected to wifi, and the phone was idling.
所以在手机空闲后,我第一次尝试连接时,http 连接会忽略 setconnectiontimeout
并永远运行,在我取消它并重试后,它每次都像魅力一样.但是有一次它不起作用它会创建一个 threadtimeout
错误,我尝试使用不同的线程,它可以工作,但我知道该线程运行了很长时间.
So after the phone is idling, the first time i try to connect, the http connection ignores the setconnectiontimeout
and runs forever, after i cancel it and try again, it works like charm everytime. But that one time that doesn't work it creates a threadtimeout
error, i tried using a different thread, it works, but i know that the thread is running for long time.
我知道 wifi 在空闲时进入睡眠状态,但我不明白为什么它会忽略 setconnectiontimeout
.
I understand that the wifi goes to sleep on idle, but i dont understand why its ignoring the setconnectiontimeout
.
任何人都可以提供帮助,非常感谢.
Anyone can help, id really appreciated.
推荐答案
不确定这是否对您有帮助,但我认为值得在这里分享.在使用超时内容时,我发现您可以分配第三种超时类型:
Not sure if this helps you, however I think it's worth sharing here. While playing with the timeout stuff I found there is a third timeout type you can assign:
// the timeout until a connection is established
private static final int CONNECTION_TIMEOUT = 5000; /* 5 seconds */
// the timeout for waiting for data
private static final int SOCKET_TIMEOUT = 5000; /* 5 seconds */
// ----------- this is the one I am talking about:
// the timeout until a ManagedClientConnection is got
// from ClientConnectionRequest
private static final long MCC_TIMEOUT = 5000; /* 5 seconds */
...
HttpGet httpGet = new HttpGet(url);
setTimeouts(httpGet.getParams());
...
private static void setTimeouts(HttpParams params) {
params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
CONNECTION_TIMEOUT);
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, SOCKET_TIMEOUT);
params.setLongParameter(ConnManagerPNames.TIMEOUT, MCC_TIMEOUT);
}
这篇关于http连接超时问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论