Apache HttpClient 超时 [英] Apache HttpClient timeout

查看:49
本文介绍了Apache HttpClient 超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法为 的整个执行指定超时时间HttpClient?

Is there a way to specify a timeout for the whole execution of HttpClient?

我尝试了以下方法:

httpClient.getParams().setParameter("http.socket.timeout", timeout * 1000);
httpClient.getParams().setParameter("http.connection.timeout", timeout * 1000);
httpClient.getParams().setParameter("http.connection-manager.timeout", new Long(timeout * 1000));
httpClient.getParams().setParameter("http.protocol.head-body-timeout", timeout * 1000);

它实际上工作得很好,除非远程主机发回数据——即使是一字节/秒——它将永远继续读取!但我想在最多 10 秒内中断连接,无论主机是否响应.

It actually works fine, except if a remote host sends back data - even at one byte/second - it will continue to read forever! But I want to interrupt the connection in 10 seconds max, whether or not the host responds.

推荐答案

目前没有办法设置最大请求持续时间:基本上你想说我不'不关心任何特定的请求阶段是否超时,但整个请求的持续时间不得超过 15 秒(例如).

There is currently no way to set a maximum request duration of that sort: basically you want to say I don't care whether or not any specific request stage times out, but the entire request must not last longer than 15 seconds (for example).

最好的办法是运行一个单独的计时器,当它到期时获取 HttpClient 实例使用的连接管理器并关闭连接,这应该终止链接.如果这对您有用,请告诉我.

Your best bet would be to run a separate timer, and when it expires fetch the connection manager used by the HttpClient instance and shutdown the connection, which should terminate the link. Let me know if that works for you.

这篇关于Apache HttpClient 超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆