The URLConnection Class
Home ] Up ] The CGI ] [ The URLConnection Class ]

 

Reader Example
Writer Example
Extended Example
Posting Form Data

 

One way to communicate with a web server is to use the URLConnection class.

The details on this class may be accessed by clicking here.

Note that, if you use an HTTP protocol-based URL, the URLConnection class will know to use HTTP when it connects, and you can use the powerful related functionality to your advantage.  For more information about the HTTP protocol, see http://www.ietf.org/rfc/rfc2068.txt

A program that uses URLConnection typically follows the following sequence of steps:

  1. Construct a URL object
  2. Call the URL object's openConnection() method to retrieve a URLConnection object for that URL.
  3. Configure the URLConnection (if necessary;  the default setup may be sufficient)
  4. Read the header fields
  5. Get an input stream and read data
  6. Get an output stream and write data
  7. Close the connection.

Note that URLConnection is an abstract class.  However, only the connect() method is actually abstract; all other methods in the class are implemented.

The Java class library provides the following subclasses of this class:

One can also implement other subclasses for other kinds of URLs (i.e., writing a protocol hander for another kind of URL).

 

The page was last updated February 19, 2008