Richard G Baldwin (512) 223-4758, baldwin@austin.cc.tx.us, http://www2.austin.cc.tx.us/baldwin/

Network Programming - The URLConnection Class

Java Programming, Lesson # 556, Revised 02/20/98.

Preface

Students in Prof. Baldwin's Advanced Java Programming classes at ACC are responsible for knowing and understanding all of the material in this lesson.

The material in this lesson is extremely important. However, there is simply too much material to be covered in detail during lecture periods. Therefore, students in Prof. Baldwin's Advanced Java Programming classes at ACC will be responsible for studying this material on their own, and bringing any questions regarding the material to class for discussion.

Introduction

I really don't plan to say very much about the URLConnection class. It is an abstract class that can be extended, and it has a protected constructor that takes a URL object as a parameter.

It has about eight variables that contain useful information about a connection.

It has about 37 methods that can be used to examine and manipulate an object of the class in a variety of different ways.

If you plan to use the URL class for the higher-lever capabilities that it offers, and you plan to write content handlers and protocol handlers, you will probably need to become very familiar with this class. In that case, you will probably want to get a copy of a good Java networking book, such as Java Network Programming, by Elliotte Rusty Howard, and study the use of this class in depth.

My objective here is simply to make you aware of the existence of the class and its many methods, and to provide some examples of how you can get and use an object of the class to obtain higher-level information about a connection.

Sample Program

This program illustrates connecting to a URL and creating a URLConnection object.

The program then uses the URLConnection object to obtain and display some of the "higher level" aspects of the URL:

The computer must be online for this program to run properly. Otherwise, it will throw an exception of type UnknownHostException.

The program was tested using JDK 1.1.3 under Win95.

The output from the program is a display of:

As of 01/19/98, an abbreviated version of the output was:
 
http://www2.austin.cc.tx.us/baldwin/Test01.html
Mon Jan 19 22:56:04 CST 1998
text/html

Code Fragments

The URLConnection class is abstract, and therefore cannot be instantiated directly. However, it can be extended, and it has a protected constructor that requires a URL object as a parameter.

A common way to get a URLConnection object is to invoke a method on a URL object that returns an object of a subclass of the URLConnection class. That is the case in the sample program for this lesson.

I will remove all exception handling code from the discussion of this program for brevity.

All of the code in the sample program for this lesson is contained in the main() method of the controlling class.

The first code fragment instantiates a URL object. This is essentially the same code that we saw in the sample programs in the lesson on the URL class, but you need to see it again here in order to understand the code that follows it.
 
      URL url = new URL(
        "http://www2.austin.cc.tx.us/baldwin/Test01.html");
The next code fragment gets a URLConnection object by invoking the openConnection() method on the URL object instantiated above.
 
      URLConnection urlConnection = url.openConnection();
The next code fragment invokes three methods on the URLConnection object to obtain three of the higher-level aspects of the URL:

The result of these three inquiries was as shown below:
 
http://www2.austin.cc.tx.us/baldwin/Test01.html
Mon Jan 19 22:56:04 CST 1998
text/html
The date information for when the file was last modified appears to be based on the date maintained by the "directory" capability of the system showing when the file was written or modified.

The content type is probably based on the file's extension.

This is the kind of information that you might need in order to make more effective use of the file pointed to by the URL. Again, there are many more methods which can be used to obtain other information about the connection.
 
      System.out.println(urlConnection.getURL());
      Date lastModified = new Date(
                          urlConnection.getLastModified());
      System.out.println(lastModified);
      System.out.println(urlConnection.getContentType());
Following this, the program uses the URL object to get an input stream and to display the contents of the file. However, this essentially duplicates a portion of the program in the earlier lesson on the URL class, so we won't discuss it further here.

Program Listing

A complete listing of the program follows.
 
/*File Url004.java Copyright 1998, R.G.Baldwin
Revised 01/19/98

Illustrates connecting to a URL and creating a
URLConnection object.

Uses the URL object to obtain and display 
the URL, the date last modified, and the content type.

Also uses the URLConnection object to obtain an 
input stream object.  Then uses this object to read and 
display the file.

Computer must be online for this program to run properly.
Otherwise, it will throw an exception of type 
UnknownHostException.

Tested using JDK 1.1.3 under Win95.

The output from the program is a display of:
the URL,
the date last modified, 
the content type, and 
the contents of the file named Test01.html in a text format

As of 01/19/98, the output(with line breaks manually 

inserted) was:

http://www2.austin.cc.tx.us/baldwin/Test01.html
Mon Jan 19 22:56:04 CST 1998
text/html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
   <TITLE></TITLE>
   <META NAME="Author" CONTENT="">
   <META NAME="GENERATOR" CONTENT="Mozilla/3.01Gold 
   (Win95; I) [Netscape]">
</HEAD>
<BODY>

<P><B><I>Richard G Baldwin (512) 223-4758, 
<A HREF="mailto:baldwin@austin.cc.tx.us">
baldwin@austin.cc.tx.us</A>,
<A HREF="http://www2.austin.cc.tx.us/baldwin/">
http://www2.austin.cc.tx.us/baldwin/</A></I></B></P>

<H3 ALIGN=CENTER>
<A HREF="http://www2.austin.cc.tx.us/baldwin/">
Test File</A></H3>

<P>This test file is used to test certain network 
programming applications.</P>

</BODY>
</HTML>  
**********************************************************/

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

class Url004{
  public static void main(String[] args){
    String dataLine;
    try{
      //Get a URL object
      URL url = new URL(
          "http://www2.austin.cc.tx.us/baldwin/Test01.html");

      //Open a connection to the URL and get a 
      // URLConnection object.
      URLConnection urlConnection = url.openConnection();
      
      //Use the connection to get and display the URL
      System.out.println(urlConnection.getURL());
      
      //Use the connection to get and display the date last
      // modified.
      Date lastModified = new Date(
                          urlConnection.getLastModified());
      System.out.println(lastModified);
      
      //Use the connection to get and display the content
      // type.
      System.out.println(urlConnection.getContentType());
          
      //Use the connection to get an InputStream object.
      // Use the InputStream object to instantiate a 
      // DataInputStream object.

      BufferedReader htmlPage = 
                 new BufferedReader(new InputStreamReader(
                                        url.openStream()));
                     
      //Use the DataInputStream object to read and display
      // the file one line at a time.
      while((dataLine = htmlPage.readLine()) != null){
        System.out.println(dataLine);
      }//end while loop
    }//end try
    catch(UnknownHostException e){
      System.out.println(e);
      System.out.println(
                        "Must be online to run properly.");
    }//end catch
    catch(MalformedURLException e){System.out.println(e);}
    catch(IOException e){System.out.println(e);}

  }//end main
}//end class Url004
//=======================================================//
-end-