如何让HTTP连接在一段时间后超时/断开?
我是 Apache HttpClient
的新手,我使用以下代码在一定时间间隔后获取 HTTP 连接超时(断开连接):
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);
但它等待超过一分钟,没有任何希望超时/断开连接?问题可能出在哪里?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HTTPClient 涉及 2 个超时,请尝试设置这两个超时,
但是,如果连接卡在本机套接字调用中,这些值将被忽略。因此,您可能必须在不同的线程中运行该请求,以便超时。请参阅我对此问题的回答,了解如何执行此操作,
java 本机进程超时
There are 2 timeouts involved in HTTPClient, try to set both,
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,
java native Process timeout
当尝试从连接管理器获取连接的操作花费太长时间时,就会触发连接管理器超时。这与 http 连接本身的超时不同。请改用 HttpClientParams.setSoTimeout()。
http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/params/HttpMethodParams.html#setSoTimeout%28int%29
The connection manager timeout triggers when the act of trying to get a connection from your connection manager takes too long. This is not the same as the timeout for the http connection itself. Use HttpClientParams.setSoTimeout() instead.
http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/params/HttpMethodParams.html#setSoTimeout%28int%29
您是否看过设置 SO_TIMEOUT ?
Have you looked at setting SO_TIMEOUT ?