15.1 Opening URLConnections


A program that uses the URLConnection class directly follows this basic sequence of steps:

  1. Construct a URL object.

  2. Invoke the URL object's openConnection( ) method to retrieve a URLConnection object for that URL.

  3. Configure the URLConnection .

  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.

You don't always perform all these steps. For instance, if the default setup for a particular kind of URL is acceptable, then you're likely to skip step 3. If you only want the data from the server and don't care about any metainformation, or if the protocol doesn't provide any metainformation, you'll skip step 4. If you only want to receive data from the server but not send data to the server, you'll skip step 6. Depending on the protocol, steps 5 and 6 may be reversed or interlaced.

The single constructor for the URLConnection class is protected:

 protected URLConnection(URL url) 

Consequently, unless you're subclassing URLConnection to handle a new kind of URL (that is, writing a protocol handler), you can only get a reference to one of these objects through the openConnection( ) methods of the URL and URLStreamHandler classes. For example:

 try {   URL u = new URL("http://www.greenpeace.org/");   URLConnection uc = u.openConnection( ); } catch (MalformedURLException ex) {   System.err.println(ex); } catch (IOException ex) {   System.err.println(ex); } 

In practice, the openConnection( ) method of java.net.URL is the same as the openConnection( ) method of java.net.URLStreamHandler . All a URL object's openConnection( ) method does is call its URLStreamHandler 's openConnection( ) method.

The URLConnection class is declared abstract. However, all but one of its methods are implemented. You may find it convenient or necessary to override other methods in the class; but the single method that subclasses must implement is connect() , which makes a connection to a server and thus depends on the type of service (HTTP, FTP, and so on). For example, a sun.net.www.protocol.file.FileURLConnection 's connect( ) method converts the URL to a filename in the appropriate directory, creates MIME information for the file, and then opens a buffered FileInputStream to the file. The connect( ) method of sun.net.www.protocol.http.HttpURLConnection creates a sun.net.www.http.HttpClient object, which is responsible for connecting to the server.

 public abstract void connect( ) throws IOException 

When a URLConnection is first constructed , it is unconnected; that is, the local and remote host cannot send and receive data. There is no socket connecting the two hosts . The connect( ) method establishes a connectionnormally using TCP sockets but possibly through some other mechanismbetween the local and remote host so they can send and receive data. However, getInputStream( ) , getContent( ) , getHeaderField( ) , and other methods that require an open connection will call connect( ) if the connection isn't yet open. Therefore, you rarely need to call connect( ) directly.

Java Network Programming
Java Network Programming, Third Edition
ISBN: 0596007213
EAN: 2147483647
Year: 2003
Pages: 164

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net