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

/** Simplest possible download of URL to get properties and contents.
 * Based on:
 * http://java.sun.com/docs/books/tutorial/networking/urls/readingURL.html
 * http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html
 * http://java.sun.com/j2se/1.4/docs/api/java/net/URL.html
 *
 * @author Bob Futrelle, Northeastern Univ.
 * @version 0.1 of 22 Feb. 2003
 */
 
public  class SimpleURLDownload {
 
        URL url, url2;
        URLConnection conn;
        String inputLine;
        BufferedReader in, in2;
        int i;

    public static void main(String[] args) {
     
     SimpleURLDownload loader = new SimpleURLDownload();
     
     loader.setup();
     
     loader.tinyRun();
     loader.seeComponents();
     }
     
     void setup() {
        try{
            url = new URL("http://www.oreilly.com"); // absolute URL
            url2 = new URL(url, "catalog/javanut4/"); // relative URL
         }
         catch(MalformedURLException e) { /* Ignore it */ }
     } // setup()
     
/** Read the content from a URL input stream and print first 5 lines. 
*/
     void tinyRun() {
         try{
            in = new BufferedReader(
                        new InputStreamReader(
                            url.openStream())); 
            in2 = new BufferedReader(
                        new InputStreamReader(
                            url2.openStream()));
             } 
                            
         catch(IOException e) {System.out.println("Can't open URL " + e);}
         
         System.out.println("\nFirst five lines of download using URL.openStream():\n");
         i = 0;
         try{
            while ((inputLine = in.readLine()) != null && i < 5)
                {System.out.println(inputLine);
                 i++;
                 }
         
        in.close(); 
        in2.close();
            }
         catch(IOException e) {System.out.println("readLine() problem " + e);}
         
         // The above worked with no problems 2/22/03.
         
     } // tinyRun()

/** Read the content from a URL with URL.openConnection and show some properties. 
*/
    void seeComponents() {
    
        try{
        
        url = new URL("http://www.oreilly.com"); // absolute URL
        conn = url.openConnection();
        
        // Now print some info
        System.out.println("\nUsing URL.openConnection() can retrieve:\n");
        System.out.println("getContentType() returns: " +
                            conn.getContentType());
        System.out.println("getContentEncoding() returns: " +
                            conn.getContentEncoding());
        System.out.println("getContentLength() returns: " +
                            conn.getContentLength());
        
        // now, get input stream from the connection        
        BufferedReader in = new BufferedReader(
                               new InputStreamReader( 
                               conn.getInputStream() ));        
                                    
         System.out.println("\nFirst five lines using URLConnection.getInputStream():\n");
         
         i = 0;
            while ((inputLine = in.readLine()) != null && i < 5)
                {System.out.println(inputLine);
                 i++;}
         
        }
        catch(Exception e) {System.out.println("Problems w. connection/info " + e);}
    
    } // seeComponents()

 } // class SimpleURLDownload
 
/* Results of the above, to standard out are:

First five lines of download using URL.openStream():

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head> 
<title>www.oreilly.com -- Welcome to O'Reilly &amp; Associates -- computer books, software conferences, online publishing</title>
<meta name="keywords" content="O'Reilly, oreilly, computer books, 

Using URL.openConnection() can retrive:

getContentType() returns: text/html
getContentEncoding() returns: text/html
getContentLength() returns: 47982

First five lines using URLConnection.getInputStream():

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head> 
<title>www.oreilly.com -- Welcome to O'Reilly &amp; Associates -- computer books, software conferences, online publishing</title>
<meta name="keywords" content="O'Reilly, oreilly, computer books, 

*/ 
 
 

