Loading Content of Web Page

Posted By : Kuldeep Kumar | 25-Feb-2018
Hey Friends, I'm Writing this Blog to Explain how you can download HTML content of a web page and store in the file on your System using java.
 
To perform this task, you should have good knowledge of URL and URLConnection classes.
 
These classes provide the capability to load the web page from the internet to out java application. The classes reside in java.net package.
 
* java.net.URL:- the instance of URL represents a valid URL of any web page resides on the internet server.
 
* methods of URL class:-
 
   1. public URLConnection openConnection()
   2. public String getPath()
   3. public String getUserInfo()
   4. public String getAuthority()
   5. public int getPort()
   7. public int getDefaultPort()
   8. public String getProtocol()
   9. public String getHost()
 
* java.net.URLConnection:- The instance of URLConnection represents to the Connection between the java application and web server it is an abstract class.
 
* methods od URLConnection class:-
 
   1. public InputStream getInputStream()
   2. public URL getURL()
   3. public String getContentType()
   4. public Object getContent()
 
you can download only unauthenticated page from a web server using this application.Am loading facebook login page.
 

public class MainApplication {
	public static void main(String args[]){
		try {
			System.out.println("sending the requst for web page");
			URL url = new URL("https://www.facebook.com"); //web url from which content will load.
			URLConnection con = url.openConnection(); //creating a url connection with server.
			System.out.println("web page loaded");
			InputStream in = con.getInputStream();  
			BufferedReader bf = new BufferedReader(new InputStreamReader(in));
			File f = new File("/home/kuldeepkumar/Videos/abc.html"); //creating an .html file to save loaded data.
			FileOutputStream fout = new FileOutputStream(f);
			String str;
			while((str=bf.readLine())!=null){
			byte b[] = str.getBytes(); //load data into file.
			fout.write(b);
			fout.flush();
		        Runtime r = Runtime.getRuntime();
			String path=f.getAbsolutePath();
			System.out.println("path : "+path);
			Process p = r.exec("/usr/bin/google-chrome"+path); //launching chrome browser with .html file.
		    }

		 catch(Exception e){
			e.printStackTrace();
		}
				
	}

}
 

 

About Author

Author Image
Kuldeep Kumar

Kuldeep is interested in developing web applications and he is currently working on Groovy on Grails. He likes to learn new technologies and love playing chess.

Request for Proposal

Name is required

Comment is required

Sending message..