Java Reference
In-Depth Information
The
InterruptedException
is meaningless to our program because it is not
multithreaded. As a result, the
InterruptedException
is ignored. However; we
must include the
catch
block because
InterruptedException
is a checked ex-
ception, and Java requires that all checked exceptions be caught.
This program is a very simple web site monitoring utility. A more “industrial strength”
version would perform some additional operations, such as:
• E-Mailing on failure
• Paging on failure
• Tracking multiple sites
As it is written, this recipe implements only the basic functionality.
Summary
This chapter showed how to use the
HttpURLConnection
class. This class adds
much more functionality than was provided by using only an
InputStream
, as was done
in Chapter 3.
Using the
HttpURLConnection
allows access to both the HTTP request and re-
sponse headers. Accessing these headers provides you with useful information about the
HTTP transaction. These headers give such information as the type of document, the web
server being used, the web browser being used and other important information.
The
HttpURLConnection
class also allows you to specify the connec-
tion and read timeouts. The connection timeout specifies the amount of time that the
HttpURLConnection
object will wait for a connection. The read timeout specifies the
amount of time that must pass before the read is terminated.
This chapter presented four recipes. The first recipe showed how to scan a site and see
the headers. The second recipe showed how to scan a range of IP addresses for web sites.
The third recipe will download either a text or binary file. The fourth recipe will monitor a
web site to make sure it does not go down.
So far we have only made use of the HTTP protocol. The HTTP protocol is not secure,
because it does not encrypt data being transmitted. For non-sensitive information this is fine.
However, when you must pass sensitive information, the HTTPS protocol is usually used.
The next chapter will discuss the HTTPS protocol.