How to fetch HTML in Java

后端 未结 5 1085
不思量自难忘°
不思量自难忘° 2020-12-05 13:35

Without the use of any external library, what is the simplest way to fetch a website\'s HTML content into a String?

5条回答
  •  有刺的猬
    2020-12-05 14:13

    I'm currently using this:

    String content = null;
    URLConnection connection = null;
    try {
      connection =  new URL("http://www.google.com").openConnection();
      Scanner scanner = new Scanner(connection.getInputStream());
      scanner.useDelimiter("\\Z");
      content = scanner.next();
      scanner.close();
    }catch ( Exception ex ) {
        ex.printStackTrace();
    }
    System.out.println(content);
    

    But not sure if there's a better way.

提交回复
热议问题