【发布时间】:2011-06-07 22:34:13
【问题描述】:
我正在尝试在 java 中读取 URL,只要 URL 在浏览器中加载它就可以工作。
但是,如果它只是在浏览器中循环而不是在我试图在浏览器中打开它时加载该页面,我的 java 应用程序就会挂起,如果有足够的时间,它可能会永远等待。如果加载超过 20 秒而我停止我的应用程序,我该如何设置超时?
我正在使用URL
这是代码的相关部分:
URL url = null;
String inputLine;
try {
url = new URL(surl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
BufferedReader in;
try {
in = new BufferedReader(new InputStreamReader(url.openStream()));
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
【问题讨论】:
-
介意发布一些相关代码吗?