UrlRetriever2.java Illustrates how the URL class can simplify communication to an HTTP server. import java.net.*; import java.io.*; /** Read a remote file using the standard URL class * instead of connecting explicitly to the HTTP server. * * Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * . * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class UrlRetriever2 { public static void main(String[] args) { checkUsage(args); try { URL url = new URL(args[0]); BufferedReader in = new BufferedReader( new InputStreamReader(url.openStream())); String line; while ((line = in.readLine()) != null) { System.out.println("> " + line); } in.close(); } catch(MalformedURLException mue) { // URL constructor System.out.println(args[0] + "is an invalid URL: " + mue); } catch(IOException ioe) { // Stream constructors System.out.println("IOException: " + ioe); } } private static void checkUsage(String[] args) { if (args.length != 1) { System.out.println("Usage: UrlRetriever2 "); System.exit(-1); } } }
UrlRetriever2.java Illustrates how the URL class can simplify communication to an HTTP server.
Permanent link to this article: http://bangla.sitestree.com/urlretriever2-java-illustrates-how-the-url-class-can-simplify-communication-to-an-http-server/