How to read webpage / URL using java code?

UPDATED: 15 September 2011
Some of you think how facebook shows the webpage. It actually retrieve the content of the page. how to read webpage in java code/Program?

Some time we need to retrieve the data of web page when there is data in XML format. You can also read the page using java here I'm putting sample code that will help you to read the page line by line.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

public class readURL  {
    public static void main(String[] args) {
        String generate_URL = "http://www.javaquery.com";
        String inputLine;
        try {
            URL data = new URL(generate_URL);
            /**
             * Proxy code start 
             * If you are working behind firewall uncomment below lines. 
             * Set your proxy server
             */

            /* Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("192.168.0.202", 8080)); */
            /* HttpURLConnection con = (HttpURLConnection) data.openConnection(proxy); */
            
            /* Proxy code end */
            
            /* Open connection */
            /* comment below line in case of Proxy */
            HttpURLConnection con = (HttpURLConnection) data.openConnection(); 
            /* Read webpage coontent */
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            /* Read line by line */
            while ((inputLine = in.readLine()) != null) {
                System.out.println(inputLine);
            }
            /* close BufferedReader */
            in.close();
            /* close HttpURLConnection */
            con.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Run the above code that will give you source code of that page. Now modify the above code as per requirement. It'll read the data in all the type. XML,HTML,etc...

Use of Proxy
You need to take care for proxy connection and it depends upon whether you are writing program for applet or desktop application. If you are writing code for applet then in production applet, you don't need proxy connection as server will have all access and will work without proxy. If you are working on desktop application that run within the Local Area Network then you must set proxy in case of firewall.

0 comments :