Using Java to read web page

The following is the code for reading html content of a web page.

Using java to read web page is different from using Java to call IE to open web pages. The visitor count for a general blog can not increase in this way. To increase visitors count, you need a program that can call IE to open web page.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
 
public class Main {
	public static void main(String[] args) {
        try {
            URL google = new URL("http://www.google.com/");
            BufferedReader in = new BufferedReader(new InputStreamReader(google.openStream()));
            String inputLine; 
 
            while ((inputLine = in.readLine()) != null) {
                // Process each line.
                System.out.println(inputLine);
            }
            in.close(); 
 
        } catch (MalformedURLException me) {
            System.out.println(me); 
 
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
    }//end main
}

This is really at the bottom level. We can simply use Jsoup to do the same thing in a easier way.

Category >> Real Methods  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

  1. DD on 2014-7-10

    But this way i cant read the dynamic contents generated by javascripts inside iframes and nested iframes?

  2. surender on 2015-4-9

    while doing openConnection, I am getting error “connection timeout”. Please provide me the code that is working fine

  3. asdasdda on 2015-7-23

    IN -> in java IN is a keyword you should use input instead.
    i had to change 3 lines of your code xD
    thx in advance

Leave a comment

*