15.1 Opening URLConnections
A program that uses the
URLConnection
class directly follows this basic sequence of steps:Construct a URL object.Invoke the URL object's
openConnection( ) method to retrieve a
URLConnection object for that URL.Configure the URLConnection.Read the header fields.Get an input stream and read data.Get an output stream and write data.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);
}
|
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 IOExceptionWhen 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.
• Table of Contents• Index• Reviews• Reader Reviews• Errata• AcademicJava Network Programming, 3rd EditionBy
Elliotte Rusty Harold Publisher: O'ReillyPub Date: October 2004ISBN: 0-596-00721-3Pages: 706
Thoroughly revised to cover all the 100+ significant updates
to Java Developers Kit (JDK) 1.5, Java Network
Programming is a complete introduction to
developing network programs (both applets and applications)
using Java, covering everything from networking fundamentals
to remote method invocation (RMI). It includes chapters on
TCP and UDP sockets, multicasting protocol and content
handlers, servlets, and the new I/O API. This is the
essential resource for any serious Java developer.
