Java 从 URL 流中读取有选择地工作
Java read from URL stream working selectively
摘要:读取URL连接的示例Java代码仅读取某些URL,而不读取其他。
详细信息:我有这个示例 Java 代码,我用它来读取 URLConnection。当URL为“http://www.example.com", the code reads the page content without any issues. However, if the URL is "http://www.cnn.com”时,不读取页面内容
public class Whosebug {
public static void main(String[] args) throws Exception {
BufferedReader inputStream = null;
try {
String urlStr = "http://www.cnn.com"; // Does not work
// urlStr = "http://www.example.com"; // **Works if this line is uncommented**
URL url = new URL(urlStr);
inputStream = new BufferedReader(new InputStreamReader(url.openStream()));
String textLine = null;
while((textLine = inputStream.readLine()) != null) {
System.out.println(textLine);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if(inputStream != null) inputStream.close();
}
}
}
CNN 从 http 重定向到 https,但您的调用不遵循重定向。您将获得一个空主体的 307,因此 readline 会导致 null 并且您的循环将被跳过。为 CNN 尝试使用 https。
摘要:读取URL连接的示例Java代码仅读取某些URL,而不读取其他。
详细信息:我有这个示例 Java 代码,我用它来读取 URLConnection。当URL为“http://www.example.com", the code reads the page content without any issues. However, if the URL is "http://www.cnn.com”时,不读取页面内容
public class Whosebug {
public static void main(String[] args) throws Exception {
BufferedReader inputStream = null;
try {
String urlStr = "http://www.cnn.com"; // Does not work
// urlStr = "http://www.example.com"; // **Works if this line is uncommented**
URL url = new URL(urlStr);
inputStream = new BufferedReader(new InputStreamReader(url.openStream()));
String textLine = null;
while((textLine = inputStream.readLine()) != null) {
System.out.println(textLine);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if(inputStream != null) inputStream.close();
}
}
}
CNN 从 http 重定向到 https,但您的调用不遵循重定向。您将获得一个空主体的 307,因此 readline 会导致 null 并且您的循环将被跳过。为 CNN 尝试使用 https。