问题描述
我是Apache HttpClient
的新手,我使用以下代码在特定时间间隔后获取HTTP连接超时(已断开连接):
I am new in Apache HttpClient
, I used the following code to get the HTTP connection timeout (disconnected) after certain time interval:
PostMethod method = new PostMethod(authURL);
HttpClient client = new HttpClient();
HttpClientParams params= new HttpClientParams();
params.setParameter(params.CONNECTION_MANAGER_TIMEOUT, 10); //10 Nano second
client.executeMethod(method);
但等待超过一分钟没有任何希望超时/断开连接?问题出在哪里?
but it wait for more than one minute without any hope to timeout/disconnect? Where can the problem be?
推荐答案
HTTPClient涉及2次超时,尝试设置两者,
There are 2 timeouts involved in HTTPClient, try to set both,
client.getHttpConnectionManager().
getParams().setConnectionTimeout(5000);
client.getHttpConnectionManager().
getParams().setSoTimeout(5000);
但是,如果连接卡在本机套接字调用中,则将忽略这些值。因此,您可能必须在不同的线程中运行请求,以便您可以将其计时。请参阅我对这个问题的答案,如何做到这一点,
However, the values will be ignored if the connection is stuck in a native socket call. So you might have to run the request in a different thread so you can time it out. See my answer to this question on how to do that,
这篇关于如何在一段时间间隔后使HTTP连接超时/断开连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!