When a user enters an HTTP-based URL (a URL starting with http: //) in a browser, It is equivalent that the Notification Server organizes an HTTP request according to the URL, and send a request to the server. At the same time, wait for the response from the server. Both requests and responses are called HTTP messages.
HTTP is divided into two parts:
1. HTTP request message.
2. HTTP Response Message.
1. HTTP Request Message
HTTP request messages are divided into request message headers and Request Entity content. The first line of the Request Message Header must consist of the following three parts:
Request methods (GET, POST, and HEAD ).
The path of the Web Resource (html "> http://www.website.com/test/test.html/test/test.html)
Part ).
HTTP Protocol version (HTTP/1.0 or HTTP/1.1 ).
The request header field is used in other lines of the request message header. The format of each row is:
"Header field" ":" "header field value"
The request header ends with a blank line. The following is an example of a GET request:
GET, HTTP, 1.1
Host: www.csdn.net
Connection: Keep-Alive
Accept :*/*
For a POST request, the object content to be submitted is placed behind the blank line in the message header. The following is an example of a POST request:
POST/servlets-examples/servlet/RequestParamExample http/ 1.1
Host: localhost: 8888
Content-Length: 29
Connection: Close
Firstname = Bill & lastname = Gates
In the preceding request message, Content-Length indicates the Length of the request Content in bytes ("firstname = Bill & lastname = Gates ). When using the POST method, this field must be provided and the length must be equal to the length of the object content. Otherwise, the server returns an error status code.
Ii. HTTP Response Message
HTTP response messages are also divided into message headers and entity content. The HTTP Response Message Header is similar to the request message header. The first line is the request result, that is, the first line in the response header indicates whether the request message has successfully obtained Web resources on the server. The first line must consist of the following three parts:
1. HTTP Version of the Response Message.
The format is HTTP/1.1 or HTTP/1.0. The version number may not be the same as the version number of the request message header.
Because the server may not support the HTTP Version described in the HTTP request. For example, if you use GET/HTTP/1.1 to request the server, when the server only supports HTTP1.0, HTTP/1.0 is returned.
2. Status Code.
This status code consists of three digits and is divided into five grades. The response code of HTTP1.1 is as follows:
(1) A number starting with 1 (1xx ).
The status code of the temporary request. It consists of 100 and 101. These status codes are not frequently used. They are mainly used as the temporary status returned by the server for the client.
(2) A number starting with 2 (2xx ).
Request success status code. The value range is from 200 to 206. The most common one is 200, which indicates that the client request is successful and the server has returned the requested Web resource to the client. The other six status codes are similar, indicating that the request is successful. They only need to guide the client and the next action. For example, Status Code 206 indicates that the server only returns a part of the requested resources. To obtain all the Web resources, the client must send an HTTP request. For more information about the meaning of other response codes, see rfc2616.
(3) A number starting with 3 (3xx ).
Web Resource redirection status code. The value range is from 300 to 307. All status codes starting with 3 change the original URL for Web Resources for different reasons and methods. For example, 302 uses a Location field to determine the Location after the Web resource changes the URL. With this status code, when the URL of a website or other Web resources changes, users accessing these Web resources cannot even know the changed URL. Therefore, you can add a 302 response when the user accesses the original URL to enable the client to automatically access the new URL.
(4) A number starting with 4 (4xx ).
Client error status code. The value range is from 400 to 417. Maybe no one wants to see this status code. But they do exist in large numbers on the Internet. If the Web resource you access does not exist or you do not have the permission to access the Web resource, the server returns this type of status code. This type of status code is most often encountered in 400. When a user sends a non-existing Web Resource path (the path following the GET, POST, and HEAD methods), the server returns this status code.
(5) A number starting with 5 (5xx ).
Server Error status code. The value range is from 500 to 505. This type of status code is also a kind of error status code, but it is different from 4xx, 5xx error is caused by the server; if you send a method that is not supported by the HTTP protocol to the server, for example, GET1, the Server Returns Error 501, indicating that the server does not support this HTTP request method.
3. Status information.
The content of the response is closely related to the status code. If the status code is 200, the status information is "OK ". The status code is 501, And the status information is "Not Implemented ". For more information about the status of each status code, see rfc2616.
The other parts of the HTTP Response Message are the same as the request message. It also consists of many response header fields, each of which occupies one line. The Response Message Header ends with a blank line, followed by the HTTP Response Message entity content. The following is an example of a complete HTTP request and response:
HTTP Request Message
GET, HTTP, 1.1
Host: www.csdn.net
HTTP Response Message
HTTP/1.0 200 OK
Content-Length: 132273
Content-Type: text/html
Content-Location: http://www.csdn.net/index.htm
Last-Modified: Sun, 28 Jan 2007 09:20:00 GMT
Accept-Ranges: bytes
ETag: "eed72b7cbd42c71: 1b0e"
Server: Microsoft-Microsoft IIS/6.0
X-Powered-By: ASP. NET
Date: Sun, 28 Jan 2007 09:23:26 GMT
Age: 32
X-Cache: HIT from cachebj244.csdn.net
Connection: close
<! DOCTYPE html
From the preceding HTTP message, we can see that the HTTP versions in the request and response messages are not the same. This indicates that the CSDN Web server adopts the HTTP1.0 protocol to be compatible with clients that only support HTTP1.0.
Note: The request method (GET, POST, etc.) in the first line of the HTTP request and response message, as well as the letters in HTTP/1.1 or HTTP1.0, must be capitalized, and the path after the request method is case sensitive. The header field is case-insensitive, but most request and response field names are capitalized.