简体   繁体   English

使用 InputStream 读取 url 的内容时遇到问题

[英]Having trouble reading in content of url using InputStream

So I run the code below and it prints ".DOCTYPE html", How do I get the content of the url?所以我运行下面的代码并打印“.DOCTYPE html”,我如何获取 url 的内容? like the html for instance?比如 html?

public static void main(String[] args) throws IOException {
        URL u = new URL("https://www.whitehouse.gov/");
        InputStream ins = u.openStream();
        InputStreamReader isr = new InputStreamReader(ins);
        BufferedReader websiteText = new BufferedReader(isr);
        System.out.println(websiteText.readLine());

    }

According to java doc https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html : "When you run the program, you should see, scrolling by in your command window, the HTML commands and textual content from the HTML file located at ".... Why am I not getting that? According to java doc https://docs.oracle.com/javase/tutorial/networking/urls/readingURL.html : "When you run the program, you should see, scrolling by in your command window, the HTML commands and textual content来自 HTML 文件,位于“....为什么我不明白?

You are only reading one line of the text.您只阅读了一行文本。 Try this and you will see that you get two lines:试试这个,你会看到你得到两行:

System.out.println(websiteText.readLine());
System.out.println(websiteText.readLine());

Try reading it in a loop to get all the text.尝试循环阅读以获取所有文本。

In your program, your did not put while loop .在您的程序中,您没有放置 while 循环

   URL u = new URL("https://www.whitehouse.gov/");
    InputStream ins = u.openStream();
    InputStreamReader isr = new InputStreamReader(ins);
    BufferedReader websiteText = new BufferedReader(isr);
    String inputLine;
    while ((inputLine = websiteText.readLine()) != null){
        System.out.println(inputLine);
   }

  websiteText.close();

BufferedReader has a method called #lines() since Java 8. The return type of #lines() is Stream.自 Java 8 以来,BufferedReader 有一个称为 #lines() 的方法。#lines() 的返回类型是 Stream。 To read an entire site you could do something like that:要阅读整个网站,您可以执行以下操作:

String htmlText = websiteText.lines()
  .reduce("", (text, nextLine) -> text + "\n" + nextLine)
  .orElse(null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM