Home | | Internet Programming | Web page retrieval

Chapter: Web or internet Programming : Java Programming

Web page retrieval

To locate and retrieve data from the network is to use the URL class.

Web page retrieval

       To locate and retrieve data from the network is to use the URL class.

 

Example: Download a Web Page

 

import java.net.*; import java.io.*;

public class RetrieveWebpage {

 

public static void main (String[] args) { if (args.length > 0) {

try {

 

//Open the URL for reading

URL u = new URL(args[0]);

InputStream in = u.openStream( );

 

//  buffer the input to increase performance

BufferedInputStream bis = new BufferedInputStream(in);

//  chain the InputStream to a Reader

 

Reader r = new

InputStreamReader(bis); int c;

 

while ((c = r.read( )) != -1) {

System.out.print((char) c);

}

}

 

catch (MalformedURLException e) { System.err.println(args[0] + " is not a parseable URL");

}

 

catch (IOException e) {

 

System.err.println(e);

}

 

} // end if

 

} // end main

} // end RetrieveWebpage

 

       The program reads a URL from the command line, opens an InputStream from that URL, chains the resulting InputStream to an InputStreamReader using the default encoding.

 

       Then uses InputStreamReader's read ( ) method to read successive characters from the file, each of which is printed on System.out. That is, it prints the raw data located at the URL:

         If the URL references an HTML file, the program's output is raw HTML.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web or internet Programming : Java Programming : Web page retrieval |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.