Reference: http://blog.csdn.net/baggio785/archive/2006/04/13/661410.aspx
Model: http://blog.csdn.net/baggio785/archive/2006/04/13/661412.aspx
HTTP request Model
1. Connect to the Web server
A client application (such as a web browser) opens a socket (80 by default) to the http port of the Web server ).
Example: http://www.myweb.com: 8080/index.html
In Java, this is equivalent to the Code:
Soceet socket = new socket ("www.myweb.com", 8080 );
Inputstream in = socket. getinputstream ();
Outputstream out = socket. getoutputstream ();
Ii. Send an HTTP request
Through connection, the client writes an ASCII text request line followed by zero or more HTTP header labels, one blank line and any data requested.
A request consists of four parts: request line, request header mark, blank line, and request data
1. Request Line: The request line consists of three tags: Request Method, request URI, and HTTP Version. They are separated by spaces.
Example: Get/index.html HTTP/1.1
The HTTP specification defines eight possible request methods:
Get retrieves a simple request that identifies a resource in a URI.
The head method is the same as the get method. The server only returns the status line and header label, and does not return the request document.
The Post Server accepts requests for data written into the output stream of the client.
The put server saves the request data as a request to specify the new content of the URI.
Delete server request to delete the resource named in URI
Options requests for server-supported request methods
The trace web server provides feedback on HTTP requests and headers.
Connect is a documented but not implemented method. It is reserved for tunnel processing.
2. Request Header: it consists of key/value pairs. Each line has a pair. The keywords and values are separated by colons.
The request header is used to notify the server of the functions and identifiers of the client. A typical request header is marked:
User-Agent client manufacturer and version
List of content types that can be recognized by the accept client
Number of bytes of Content-Length appended to the request
3. blank line: the last request header is followed by an empty line, which sends a carriage return and a fallback message, notifying the server that there is no header mark below.
4. Request data: the Content-Type and Content-Length headers are usually used for data transmission using post.
3. The server accepts the request and returns an HTTP Response
The Web server parses the request and locates the specified resource. The server writes a copy of the resource to the socket, which is read by the client.
A response consists of four parts: Status line, Response Header mark, blank line, and response data.
1. Status line: the Status line consists of three tags: HTTP Version, response code, and response description.
HTTP Version: specify the maximum version that the client can understand.
Response Code: A three-digit numeric code that indicates whether the request succeeds or fails. If the request fails, it indicates the reason.
Response Description: the readability of the response code.
Example: HTTP/1.1 200 OK
HTTP response code:
1xx: information, request received, continue processing
2XX: Successful, accepted, understood, and accepted
3xx: redirection. actions that must be performed further to complete the request
4xx: client error:
2. Response Header: like the request header, they indicate the functions of the server and identify the details of the response data.
3. blank line: the last response header is followed by an empty line. The carriage return and fallback are sent, indicating that no header mark is available below the server.
4. Response Data: HTML documents and images, that is, HTML itself.
4. The server closes the connection and the browser parses the response.
1. the browser first parses the status line to check the status code indicating whether the request is successful.
2. parse each response header, and the header Mark tells the following several bytes of HTML.
3. Read the response data HTML, format it according to the syntax and semantics of the HTML, and display it in the browser window.
4. An HTML document may contain references to other resources to be loaded. the browser recognizes these references and makes additional requests to other resources. This process is repeated multiple times.
5. Stateless connection
The HTTP model is stateless, indicating that the Web server does not remember requests from the same client when processing a request.
Vi. Instances
1. the browser sends a request
GET/index.html HTTP/1.1
Server Response
HTTP/1.1 200 OK
Date: Apr 11 2006 15:32:08 GMT
Server: Apache/2.0.46 (win32)
Content-Length: 119
Content-Type: text/html
<HTML>
<Head>
<LINK rel = "stylesheet" href = "index.css">
</Head>
<Body>
</Body>
</Html>
2. the browser sends a request
Get.index.css HTTP/1.1
Server Response
HTTP/1.1 200 OK
Date: Apr 11 2006 15:32:08 GMT
Server: Apache/2.0.46 (win32)
Connection: keep-alive, close
Content-Length: 70
Content-Type: text/Plane
H3 {
Font-size: 20px;
Font-weight: bold;
Color: # 005a9c;
}
3. the browser sends a request
Get image/logo.png HTTP/1.1
Server Response
HTTP/1.1 200 OK
Date: Apr 11 2006 15:32:08 GMT
Server: Apache/2.0.46 (win32)
Connection: keep-alive, close
Content-Length: 1280
Content-Type: text/Plane
{Binary image data follows}
(Appendix)
1. HTTP specification: RFC published by the Internet Engineering Development Organization (IETF) specifies Internet standards, which are widely accepted by Internet research and development institutions. Because they are standard documents, they are generally written in formal languages, like the legislative document.
2. RFC: once an RFC is proposed, it is numbered and will not be changed. When a standard is modified, a new RFC is given. As a standard, RFC is widely used on the Internet.
3. Several important RFC for http:
Rfc1945 HTTP 1.0 description
Rfc2068 initial description of HTTP 1.1
Rfc2616 standard for HTTP 1.1
4. Resource Identifier uri (Uniform Resource identifter, Uri)
HTTP reference
I. Http code response code
The response code consists of three decimal digits that appear in the first line of the response sent by the HTTP server.
There are five types of response codes, represented by their first digit:
1.1xx: information, request received, continue processing
2.2xx: Successful, accepted, understood, and accepted
3.3xx: redirection. actions that must be performed further to complete the request
4.4xx: client error. The request contains a syntax error or the request cannot be implemented.
5.5xx: server error. The server cannot implement an obviously invalid request.
The following table shows each response code and its meaning:
100 continue
101 group Exchange Association
200 OK
201 created
202 accepted
203 unauthorized information
204 NO content
205 reset content
Part 1
300 multiple options
301 permanent transfer
302 found
303 see other
304 not modified
305 use proxy
307 temporary redirection
400 Error request
401 unauthorized
402 payment required
403 Forbidden
404 not found
405 methods not allowed
406 not accepted
407 proxy authorization required
408 request timeout
409 conflict
410 expired
411 Length
412 precondition is not true
413 the request instance is too large
414 the request URI is too large
415 unsupported media types
416 request range not met
417 failure expectation
500 Internal Server Error
501 unused
502 Gateway error
503 unavailable services
504 gateway timeout
505 HTTP Version Not Supported
Ii. HTTP Header
The header is composed of primary key/value pairs. They describe the properties of the client or server, the transferred resources, and the connection to the server.
Four different types of header labels:
1. General header: it can be used for requests or responses. It is associated with transactions as a whole rather than a specific resource.
2. Request Header: allows the client to pass information about itself and the desired response form.
3. Response Header: the server and the response that transmits its own information.
4. Object Header: defines the information of the transferred resource. Can be used for requests or responses.
Header Format: <name >:< value> <CRLF>
The following table describes the header labels used in HTTP/1.1.
Accept defines the media types that can be processed by the client, sorted by priority;
You can define multiple types and use wildcards in a comma-separated list. For example: accept: image/JPEG, image/PNG ,*/*
Accept-charset defines the character sets that can be processed by the client, sorted by priority;
You can define multiple types and use wildcards in a comma-separated list. Example: Accept-charset: iso-8859-1, *, UTF-8
Accept-encoding defines the encoding mechanism that the client can understand. Example: Accept-encoding: gzip, compress
Accept-language defines the natural language list that the client is willing to accept. Example: Accept-language: En, de
An accept-ranges response header that allows the server to specify that the request will be accepted for the resource component at the given offset and length.
The value of this header is considered as the measurement unit of the Request range. For example, accept-ranges: bytes or accept-ranges: None
Age allows the server to specify the length of time that has elapsed since the server generated the response, in seconds.
This header is mainly used to cache responses. Example: Age: 30
Allow is a response header that defines a list of HTTP methods supported by the secondary source in the request URI. Example: Allow: Get, put
Authorization: A Response Header that defines the authorization required to access a resource (domain and encoded user ID and password ).
Example: Authorization: Basic yxv0ag9yonboaww =
Cache-control a general header used to define cache commands. Example: cache-control: Max-age = 30
Connection indicates whether to save the socket connection as an open general header. For example, connection: close or connection: keep-alive
Content-base is an object header that defines the basic URI. to parse the object relative to URLs within the object range.
If the content-base header is not defined to parse relative URLs, use the content-location uri (exists and is absolute) or use the URI request.
For example, content-base: http: // www.myweb.com
Content-encoding is a media type modifier that specifies how an object is encoded. Example: Content-encoding: Zip
Content-language is used to specify the natural language type of data in the input stream. For example, content-language: En
Content-Length specifies the length of bytes contained in the data in the request or response. Example: Content-Length: 382
Content-location specifies the resource location (URI) contained in the request or response ).
For example. The URL is also the starting point of the relative URL of the object to be parsed.
Example: Content-location: http://www.myweb.com/news
An MD5 Digest of A Content-MD5 entity used as a Checksum.
Both the sender and receiver calculate the MD5 Digest, and the value calculated by the receiver is compared with the value passed in the header.
Example: Content-MD5: <base64 of 128 MD5 Digest>
Content-range is sent along with some objects. It indicates the offset between the low and high bytes of the inserted bytes, and the total length of the object.
Example: Content-range: 1001-2000/5000
Contern-type indicates the MIME type of the sent or received object. For example, Content-Type: text/html
The date on which the HTTP message is sent. Example: Date: Mon, 10pr 18:42:51 GMT
Etag is an entity header that assigns a unique identifier to the sent resource.
For resources that can use multiple URL requests, etag can be used to determine whether the actually sent resource is the same resource.
Example: etag: "208f-419e-30f8dc99"
Expires specifies the object validity period. Example: expires: Mon, 05 Dec 2008 12:00:00 GMT
Form is a request header that specifies the email address of the manual user who controls the user proxy. Example: From: [email protected]
Host Name of the requested resource. This domain is mandatory for requests that use HTTP/1.1. Example: Host: www.myweb.com
If-modified-since contains a GET request, the request is conditionally dependent on the date when the resource was last modified.
If the header mark is displayed and the resource has been modified since the specified date, a 304 response code should be returned.
Example: If-modified-since: Mon, 10pr 18:42:51 GMT
If-Match: if a request is contained, one or more object tags are specified. Only the etag of the instance is sent to the resource marked with a partition in the list.
Example: If-Match: "208f-419e-308dc99"
If-None-Match: if a request is contained, one or more object tags are specified. The resource's etag does not match any of the conditions in the list before the operation is executed.
Example: If-None-match: "208f-419e-308dc99"
If-range specifies an object tag of a resource, and the client already owns a copy of the resource. Must be used with the range header.
If the object has not been modified since it was last retrieved by the client, the server sends only the specified range. Otherwise, the server sends the entire resource.
Example: range: byte = 0-499 <CRLF> If-range: "208f-419e-30f8dc99"
If-unmodified-since returns this object only when the requested object has not been modified since the specified date.
Example: If-unmodified-since: Mon, 10pr 18:42:51 GMT
Last-modified specifies the date and time when the requested resource was last modified. Example: Last-modified: Mon, 10pr 18:42:51 GMT
Location: a resource that has been moved is used to redirect the requester to another location.
Used with status code 302 (temporarily moved) or 301 (permanently moved.
Example: Location: http://www2.myweb.com/index.jsp
Max-forwards: A request header used for the TRACE method to specify the maximum number of proxies or gateways. This request can be routed through the gateway.
The number of proxies or gateways should be reduced before passing requests. Example: Max-forwards: 3
Pragma is a common header that sends implementation-related information. Example: Pragma: No-Cache
Proxy-authenticate is similar to www-authenticate, and is intended to request authentication only from the next server of the Request chain (proxy.
Example: proxy-Authenticate: Basic realm-Admin
Proxy-proxy-authorization is similar to authorization, but it does not intentionally pass anything further than in the instant server chain.
Example: proxy-authorization: Basic yxv0ag9yonboaww =
The public List displays the method sets supported by the server. Example: public: Options, mget, mhead, get, head
Range specifies the offset range of a measurement unit and a part of the requested resource. Example: range: bytes = 206-5513
Refener is a request header field that indicates the initial resource that generates the request. For an HTML form, it contains the address of the web page of the form.
Example: refener: http://www.myweb.com/news/search.html
Retry-after is a response header field that is sent by the server in combination with status code 503 (unable to provide services) to indicate how long it should wait before the request is resumed.
This time can be a date or a unit of seconds. Example: retry-after: 18
Server indicates the header of the web server software and its version number. Example: SERVER: Apache/2.0.46 (win32)
Transfer-encoding is a common header label that indicates the type of the message body to be reversed by the recipient. Example: Transfer-encoding: chunked
Upgrade allows the server to specify a new protocol or Protocol version, which can be used with response code 101 (switching protocol.
Example: Upgrade: HTTP/2.0
The User-Agent defines the software type (such as a web browser) used to generate requests ).
Example: User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT; digext)
Vary is a response header label used to select a response entity from the available response representation using server-driven negotiation. Example: vary :*
A common header that contains all intermediate hosts and protocols is used to meet the request. Example: via: 1.0 fred.com, 1.1 wilma.com
Warning is used to provide the Response Header mark for response status supplement information. Example: Warning: 99 www.myweb.com piano needs tuning
WWW-Authenticate: A Response Header that prompts the user agent to provide the user name and password. It can be used with Status Code 401 (unauthorized. Returns an Authorization header.
Example: www-Authenticate: Basic realm = zxm. Mgmt
From http://www.cnblogs.com/riky/archive/2007/04/09/705848.html
[Transfer] HTTP request model and header information reference