An HTTP Client
Example 5-6
shows a program, HttpClient
, that
downloads the contents of a URL from a web server and writes it to a
file. It behaves like the GetURL
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, HttpClient
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
protocols.
At its heart, HttpClient
is
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
plain 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
is given. 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 ...
Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.