shows a program,
downloads the contents of a URL from a web server and writes it to a
file. It behaves like the
program from Example 5-1.
Despite the similarity in behavior, however, the implementation of
these two programs is entirely different. Whereas
GetURL relies on the
URL class and its protocol handlers to
handle protocol details,
connects directly to a web server and communicates with it using the
HTTP protocol. As a consequence,
HttpClient is restricted to downloading URLs
that use the http: or https:
protocol. It can't handle ftp: or other network
At its heart,
much like the
Connect program of
Example 5-4: it connects to a
server, sends some text, and reads the response. This example is more
complex, however, for three main reasons. First, it supports the
https: protocol in addition to
HTTP, demonstrating how to do
networking with SSL secure sockets (a feature new in Java 1.4).
Second, it sends an HTTP request to the server, which is more complex
than the single line sent by
Connect. Third, it doesn't just read and
print the server's response; instead, it parses it to separate the
HTTP response headers from the document content that follows.
A feature to note in Example
5-6 is its use of the
java.net.URI class to parse the HTTP URL it
URI is new in Java 1.4.
It has more powerful URL parsing features than the
URL class we've already seen, but has none of the networking ...