gpt4 book ai didi

java - 使用返回 `java.io.InputStream` 的可靠超时读取 http 页面的类

转载 作者:可可西里 更新时间:2023-11-01 17:03:42 24 4
gpt4 key购买 nike

是否有任何用于读取返回 java.io.InputStream 且其超时可靠的 http 页面的类?
我尝试了 java.net.URLConnection 并且它没有可靠的超时(它需要更多的时间来设置超时)?我的代码在这里:

        URLConnection con = url.openConnection();
con.setConnectTimeout(2000);
con.setReadTimeout(2000);
InputStream in = con.getInputStream();

最佳答案

我认为超时对您不起作用的原因是您在建立连接后设置超时,或者您使用了错误的 setter 。也有可能您使用的是“非标准”版本的 URLConnection ...

"Some non-standard implementation of this method ignores the specified timeout. To see the read timeout set, please call getReadTimeout()." (or getConnectTimeout())

如果您发布了实际代码的相关部分,我们可以为您提供更好的答案...


或者,使用 Apache HttpClient 库。

关于java - 使用返回 `java.io.InputStream` 的可靠超时读取 http 页面的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8787324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com