// Reading a WWW page trying again if the URL is malformed // Barry Cornelius, 23rd November 1999 import java.io. BufferedReader; import java.io. InputStream; import java.io. InputStreamReader; import java.io. IOException; import java.net. MalformedURLException; import java.util. Properties; import java.net. URL; public class WWWTryAgain { public static void main(final String[] pArgs) { InputStream tInputStream = iGetInputStreamForWebPage(); iReadTheWebPage(tInputStream); } private static InputStream iGetInputStreamForWebPage() { // you will need to uncomment the following three statements // if your access to the WWW is through a proxy machine // you will have to replace proxymachine.sitename.com // and 8080 with appropriate values for your site // final Properties tProperties = System.getProperties(); // tProperties.put("http.proxyHost", "proxymachine.sitename.com"); // tProperties.put("http.proxyPort", "8080"); final BufferedReader tKeyboard = new BufferedReader(new InputStreamReader(System.in)); String tUrlString = null; URL tUrl = null; InputStream tInputStream = null; while (true) { System.out.print("URL? "); try { tUrlString = tKeyboard.readLine(); tUrl = new URL(tUrlString); tInputStream = tUrl.openStream(); break; } catch(MalformedURLException pMalformedURLException) { System.out.println("MalformedURLException occurred"); } catch(IOException pIOException) { System.out.println("IOException occurred"); System.exit(2); } } return tInputStream; } private static void iReadTheWebPage(InputStream pInputStream) { final BufferedReader tWebPage = new BufferedReader(new InputStreamReader(pInputStream)); try { while (true) { String tLine = tWebPage.readLine(); if ( tLine == null ) { break; } System.out.println(tLine); } } catch(IOException pIOException) { System.out.println("IOException occurred later"); System.exit(3); } } }