javahtmlreaderbuffered

mIn java, how would one go about loading a web page into a BufferedReader, as mine will not print?


Hey I a having a little trouble here. I am doing File Writing at school and we got the challenge of reading a webpage. How is it possible to do it? I had a go with a JSoup and an Apache plugin, but neither worked, but I have to use the net import

I am a bit of a noob at coding, so there will probably be a couple of errors! Here is my code:

    URL oracle = new URL("http://www.oracle.com/");
    BufferedReader br = new BufferedReader(new InputStreamReader(oracle.openStream()));

    String inputLine;
    while ((inputLine = br.readLine()) != null){
         System.out.println(inputLine);
    }
    br.close();

There is no output from the program, and earlier I managed output but it was in the form of HTML, however I deleted that code, ironically looking for a fix for that issue.

Any help or solutions would be greatly appreciated! Thank you all very much!


Solution

  • The code example is from Reading Directly from a URL, but the tutorial is old. The url http://www.oracle.com now redirects to https://www.oracle.com/ but you don't follow the redirect.

    If you use a URL that does not redirect, like http://www.google.com you will see that the code works.

    If you want a more robust program that handles redirects, you'll probably want to use a HttpURLConnection instead of the basic URL, as it has more features for you to use.