URL.openConnection 方法读取网页会产生阻塞,
采用异常 或concurrent包里的future等均无效;
搜到用Socket的.setOsTimeOut(); 可以设置时间,但是自己要写不少请求的文件头;
最后看到这篇,URL有类似的方法:
[url]http://stuffthathappens.com/blog/2007/09/10/urlopenstream-might-leave-you-hanging/[/url]
主要代码如下:
[quote]
采用异常 或concurrent包里的future等均无效;
搜到用Socket的.setOsTimeOut(); 可以设置时间,但是自己要写不少请求的文件头;
最后看到这篇,URL有类似的方法:
[url]http://stuffthathappens.com/blog/2007/09/10/urlopenstream-might-leave-you-hanging/[/url]
主要代码如下:
[quote]
URLConnection conn = url.openConnection();
// setting these timeouts ensures the client does not deadlock indefinitely
// when the server has problems.
conn.setConnectTimeout(timeoutMs);
conn.setReadTimeout(timeoutMs);
in = conn.getInputStream();
[/quote]
本文探讨了在使用URL.openConnection方法读取网页时遇到的阻塞问题,通过设置连接超时和读取超时,避免了客户端死锁。提供了解决方案及关键代码示例。
1065

被折叠的 条评论
为什么被折叠?



