javahtmlscreen-scraping

How to fetch HTML in Java


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


Solution

  • 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.