Java 网页 读取 阻塞 超时的解决方法
Java 网页 读取 阻塞 超时的解决办法
URL.openConnection 方法读取网页会产生阻塞,
采用异常 或concurrent包里的future等均无效;
搜到用Socket的.setOsTimeOut(); 可以设置时间,但是自己要写不少请求的文件头;
最后看到这篇,URL有类似的方法:
http://stuffthathappens.com/blog/2007/09/10/urlopenstream-might-leave-you-hanging/
主要代码如下:
URL.openConnection 方法读取网页会产生阻塞,
采用异常 或concurrent包里的future等均无效;
搜到用Socket的.setOsTimeOut(); 可以设置时间,但是自己要写不少请求的文件头;
最后看到这篇,URL有类似的方法:
http://stuffthathappens.com/blog/2007/09/10/urlopenstream-might-leave-you-hanging/
主要代码如下:
引用
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();