7.3.4 URLs

Suppose you have a string that represents a URL (i.e. an address of a file on the web, together with the protocol that may access it). To get the contents you first convert the string to an instance of the URL class. If you make a connection with the URL-object, the connection is represented by an instance of the URLConnection class. This class in its turn offers a method getInputStream that returns an input stream. So now we can use the stream zoo again.

Since the network is not always very prompt, it makes sense to give the stream first the functionality of a BufferedInputStream and then e.g. of a DataInputStream. If we sprinkle this with the necessary exception-catches, we get the following code.

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

public class readURL {

  static URL url;
  static URLConnection connection;
  static DataInputStream in;
  
  public static void main (String[] args) {
    if (args.length != 1 ) 
      System.out.println("usage: java getURL <URLaddress>");
    else {
      try {
	url = new URL((String) args[0]);
	connection = url.openConnection();
	readFromURL(connection);
      } catch (MalformedURLException e) {
	System.out.println("Invalid URL");
	System.exit(0);
      } catch (IOException e) {
	System.out.println("cannot access URL");
	System.exit(0);
      }
    }
  }

  static void readFromURL (URLConnection connection) {

    System.out.println("content type = " + connection.getContentType());
    System.out.println("content length = " + connection.getContentLength());
    
    try {
      in = new DataInputStream(new 
		     BufferedInputStream(connection.getInputStream()));
      String s = null;
      while ((s = in.readLine()) != null) {
	System.out.println(s);
      }
    } catch (IOException e) {
      System.out.println("stream error");
    }
  }
}