Java Reference
In-Depth Information
Connecting to a Web Server
Example 5-4 shows a program, HttpClient , that downloads the contents of a URL
from a web server and writes it to a file or to the console. It behaves just like the
GetURL program from Example 5-1 does. Despite the similarity in behavior, how-
ever, the implementation of these two programs is entirely different. While GetURL
relies on the URL class and its protocol handlers to handle protocol details, Http-
Client connects directly to a web server and communicates with it using the
HTTP protocol. (It uses an old and extremely simple version of the protocol,
which keeps the program very simple.) As a consequence, HttpClient is restricted
to downloading URLs that use the http: protocol. It can't handle ftp: or other net-
work protocols. Note that HttpClient does use the URL class but only to represent
a URL and to parse it, not to connect to it.
The main point of interest in this example is the introduction of the Socket class,
which creates a stream-based network connection between a client and a server.
To create a network connection to another host, you simply create a Socket , spec-
ifying the desired host and port. If there is a program (a server) running on the
specified host and listening for connections on the specified port, the Socket()
constructor returns a Socket object you can use to communicate with the server.
(If there is not a server listening on the specified host and port, or if anything goes
wrong—and many things can go wrong with networking—the Socket() construc-
tor throws an exception).
If you are not familiar with hosts and ports, think of the host as a post office and
the port as a post-office box. Just as a post office has many different post-office
boxes, any host on the network can run many different servers at a time. Different
servers use different ports for their addresses. To establish a connection, you must
specify both the correct host and the correct port. Many services have standard
default ports. Web servers run on port 80, POP email servers run on port 110, and
so on.
Once you have a Socket object, you are connected, across the network, to a
server. The getInputStream() method of the socket returns an InputStream you
can use to read bytes from the server, and getOutputStream() returns an Output-
Stream you can use to write bytes to the server. This is exactly what the Http-
Client program does. It establishes a connection to the web server, sends an
HTTP GET request to the server through the output stream of the socket, and then
reads the server's response through the input stream of the socket. Note once
again that the GET request is explicitly terminated with a “\n” newline character,
rather than relying on the platform-dependent line terminator provided by the
println() method.
Example 5•4: HttpClient.java
package com.davidflanagan.examples.net;
import java.io.*;
import java.net.*;
/**
* This program connects to a Web server and downloads the specified URL
* from it. It uses the HTTP protocol directly.
**/
Search WWH ::




Custom Search