Java Reference
In-Depth Information
Exercises
5-1. Using the
URLConnection
techniques demonstrated in Example 5-2, write a
program that prints the modification date of a specified URL.
5-2. Modify the
HttpClient
program of Example 5-4 so that it uses a newer ver-
sion of the HTTP protocol. To do this, you have to send a somewhat more
complicated
GET
request to the web server. Use the
HttpMirror
program of
Example 5-5 to find what form this request should take. HTTP Versions 1.0
and later add a version number to the
GET
request and follow the
GET
line
with a number of header lines followed by a blank line that serves to termi-
nate the request.
For this exercise, the only header you need to include is the User-Agent line,
which should identify the web client that you are using. Since you are writing
your own web client, you can give it any name you like! Be sure to follow
your
GET
request and User-Agent header with a blank line, or the web server
will keep waiting for more headers and will never respond to your request.
When you get this program working, you should notice that web servers
respond differently to requests from it than they did to requests from the orig-
inal
HttpClient
program. When a client requests data using HTTP 1.0 or 1.1,
the server sends a version number, a status code, and a number of response
header lines before it sends the actual requested file.
5-3. Write a simple server that reports the current time (in textual form) to any
client that connects. Use Example 5-5,
HttpMirror
, as a framework for your
server. This server should simply output the current time and close the con-
nection, without reading anything from the client. You need to choose a port
number that your service listens on. Use the
GenericClient
program of
Example 5-8 to connect to this port and test your program. Alternatively, use
the
HttpClient
program of Example 5-4 to test your program. To do this,
encode the appropriate port number into the URL.
HttpClient
sends an
extraneous
GET
request to the time server, but it still displays the server's
response.
5-4. In the discussion of Example 5-8,
GenericClient
, there is an example of
using that program to communicate with a POP (Post Office Protocol) server
to retrieve email. The POP protocol is a simple one; a little experimentation
with
GenericClient
should allow you to figure out how it works. (Be careful
not to delete any important email!)
For this exercise, write a client program named
Checkmail
that uses the POP
protocol to check a user's mail. It should output the number of messages that
are waiting to be retrieved and display the From line of each message. This
client should
not
use the POP
dele
command to delete mail messages from
the server; it should simply display a summary of the messages waiting to be
retrieved. In order to read mail messages, this client has to know the host-
name of the POP server and has to send a username and password to the
server. Your program may obtain the hostname, username, and password
from the command line or by prompting the user, but should ideally get this
information by reading a configuration file. Consider a
java.util.Properties
object to implement such a configuration file.