出于某种原因,当我发出 GET 请求时,它似乎进入了无限循环。我认为这是我的网络应用程序有问题,但在尝试 google.com 后,出现了相同的结果。
try {
URL url = new URL("http://google.com");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(10000 /* milliseconds */);
con.setConnectTimeout(15000 /* milliseconds */);
con.setRequestMethod("GET");
con.setDoInput(true);
con.connect();
InputStream is = con.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
for (String line = reader.readLine(); line != null;) {
System.out.println(line);
}
reader.close();
} catch (ClientProtocolException e) {
System.out.println("Client Exception " + e.getMessage());
} catch(IOException e) {
e.printStackTrace();
System.out.println("IOException " + e.getMessage());
}
这段代码永远不会通过 for 循环。它只是继续打印。任何人都看到有什么问题吗?