Excerpted from <How Tomcat works>.
HTTP Requests:
A HTTP request consists of three components:
- Method-Uniform Resource Identifier(URI)-Protocol/Version
- Request headers
- Entity body
An example of a HTTP request is the following:
GET /index.html HTTP/1.1
Accept: */*
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 1.0.3705; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; OfficeLiveConnector.1.3; OfficeLivePatch.0.0; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Host: localhost:8080
Connection: Keep-Alive
The method-URI-protocol version apprears as the first line of the request.
GET /index.html HTTP/1.1
Where GET is the request method, /index.html represents the URI and HTTP/1.1 is the Protocol/Version section.
Each HTTP request can use one of the many request methods as specified in the HTTP standards. The HTTP 1.1 supports seven types of request: GET, POST, HEAD, OPTIONS, PUT, DELETE and TRACE. GET and POST are the most commonly used in Internet application.
The URI specifies an Internet resource completely. A URI is usually interpreted as being relative to the server's root directory. Thus, it should always begin with a forward slash /. A Uniform Resource Location(URL) is actually a type of URI. The protocol version represents the version of the HTTP protocol being used.
The request header comtains useful information about the client environment and the entity body of the request. For example, it could contain the language the browser is set for, the length of th entiry body, and so on. Each header is separated by a carriage return/linefeed(CRLF) sequence.
Between the headers and the entity body, there is a blank line(CRLF) that is important to the HTTP request format. The CRLF tell the HTTP server where the entiry body begins. In some Internet programming books, this CRLF is considered the fourth component of an HTTP request.
HTTP Response
Similar to a HTTP request, an HTTP response also consists of three parts:
- Protocol-Status code-Description
- Response headers
- Entiry body
The following is an example of a HTTP response:
HTTP/1.1 200 OK
Server: Microsoft-IIS/4.0
Date: Mon, 5 Jan 2004 13:13:33 GMT
Content-Type: text/html
Last-Modified: Mon, 5 Jan 2004 13:13:12 GMT
Content-Length: 112
<html>
<head>
<title>HTTP Response Example</title>
</head>
<body>
Welcome to Brainy Software
</body>
</html>
The first line of the response header is similar to the first line of the request header. The first line tells you that protocol used is HTTP version 1.1, the request succeeded(200=success), and that everything went okay.
The response headers contain useful information similar to the haeders in the request.