gpt4 book ai didi

java - Http 连接读取超时

转载 作者:行者123 更新时间:2023-11-29 05:24:02 24 4
gpt4 key购买 nike

亲爱的 friend 们,我希望代码设置给定 URL 的响应时间,特别是给定的秒数,否则它应该是错误的

我在这里附上了我的代码,它有 730MB 的 Iso 文件下载页面,我最少需要 30 分钟。在这里,我只给了 10 秒的时间来下载文件并从该页面获得响应,我确信 30 分钟的下载文件不会在 10 秒内给出响应,但这里它给出了正确的响应。

请在这里检查我的代码并告诉我我在哪里做错了..

public class test {

public static void main(String[] args) throws ClientProtocolException, IOException {
// TODO Auto-generated method stub
nodeBIT("");
}
private static void nodeBIT(String string) throws ClientProtocolException, IOException {
String url1 = "http://download.microsoft.com/download/e/e/9/ee9d0116-c9fe-4fc2-b59c-406cbfb6d515/xpsp3_5512.080413-2113_usa_x86fre_spcd.iso";
URL obj = new URL(url1); URLConnection con = obj.openConnection();
con.setConnectTimeout(10000);
con.setReadTimeout(10000); // 10 seconds to read the file
InputStreamReader input = new InputStreamReader(con.getInputStream());
BufferedReader in = new BufferedReader(input);
String inputLine;
String fullline = "";
while ((inputLine = in.readLine()) != null)
{
fullline = fullline.concat(inputLine);
}
System.out.println(fullline);

}
}

最佳答案

只要正在接收数据,它就不会根据 javadocs 超时

你可以修改你的循环

    long se = System.currentTimeMillis();
while ((inputLine = in.readLine()) != null)
{
fullline = fullline.concat(inputLine);
if ( System.currentTimeMillis() > se + 10000) {
throw new IOException ();
}
}

关于java - Http 连接读取超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23422650/

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