Java Reference
In-Depth Information
Review of HTTP
This section presents a brief review of the Hypertext Transfer Protocol. The whole story is in
RFC 2616; this section covers the essentials.
Requests and Responses
HTTP is built around requests and responses. A client sends a request to a server—something like,
“Please give me such-and-such HTML page.” The server sends back a response—something
like, “Here's the file,” or, “I don't know what you're talking about.”
Requests and responses have two parts: headers and content. If you type a URL into your
browser, the browser creates an HTTP request (mostly headers) and sends it to a server. The
server finds the requested file and sends it back in an HTTP response. The response headers
describe things like the type of web server, the file type of the response, the length of the response,
and other information. The response content is the data of the file itself.
Parameters
Browsers and other HTTP clients request specific named resources from HTTP servers. In
addition, clients can pass parameters to the server. Parameters are simple name and value
pairs. For example, a client might send a userid parameter with a value of “jonathan” to a
server. HTTP also supports passing binary data to the server in the body of a request, and the
Java stream classes make it easy to exchange a variety of data types.
When a browser is the HTTP client, parameters are generally collected from HTML forms.
You've seen these forms, like the one in which you fill in your shipping address and your credit
card number. Form values are sent as parameters to a web server when you click the Submit or
Next button on the form.
The client encodes parameters before they are sent to the server. Parameters are passed as
name and value pairs; multiple parameters are separated by ampersands. The exact way that
parameters are encoded is documented in the J2SE documentation for java.net.URLEncoder .
The rules are relatively simple.
1.
The space character is converted to a plus (+) sign.
2.
The following characters remain unchanged: lowercase letters a through z, uppercase
letters A through Z, the numbers 0 through 9, the period (.), the hyphen (-), the asterisk
(*), and the underscore (_).
3.
All other characters are converted into “%xy”, where “xy” is a hexadecimal number that
represents the low 8 bits of the character.
GET, HEAD, and POST
The simplest HTTP operation is GET. This is what happens when you type a URL into your
browser. The browser says, “GET me this URL,” and the server responds with the headers and
content of the response.
With a GET request, parameters are added to the end of the URL in encoded form. (Some
servers have trouble with very long URLs; if you have a lot of parameters, or binary data, you
Search WWH ::




Custom Search