HTTP response message and working principle

Source: Internet
Author: User

HTTP is a request/response protocol that sends a request to the server after a client has established a connection with the server, and responds to the request after the server has received it.

Hypertext Transfer Protocol (hypertext Transfer Protocol, referred to as HTTP) is the application layer protocol. HTTP is a request/response protocol that sends a request to the server after a client has established a connection with the server, and responds to the request after the server has received it.

HTTP Request Message

The HTTP request message consists of 4 parts of the request line, the request header, the blank line, and the request package, as shown in:

The following is a simple analysis of the request message format:

Request line: The request line consists of the Method field, the URL field, and the HTTP protocol version field 3 parts, separated by a space. Common HTTP Request methods are get, POST, HEAD, PUT, DELETE, OPTIONS, TRACE, CONNECT;

Get: Use the Get method when the client wants to read a resource from the server. The GET method requires the server to place the URL-positioned resource in the data portion of the response message, which is sent back to the client, requesting a resource from the server. When using the Get method, the request parameter and the corresponding value are appended to the URL, using a question mark ("?") Represents the end of the URL and the start of the request parameter, which is limited by the length of the pass parameter. For example,/index.jsp?id=100&op=bind.

Post: When the client provides more information to the server, the Post method can be used to submit data to the server, such as completing the submission of form data and submitting the data to the server for processing. Get is typically used to get/query resource information, and POST comes with user data, which is typically used to update resource information. The POST method encapsulates the request parameters in the HTTP request data, appears as a name/value, and can transmit large amounts of data;

Request Header: The request header consists of a keyword/value pair, one pair per line, a keyword and a value separated by the English colon ":". The request header notifies the server that there is information about the client request, and the typical request headers are:

Blank line: After the last request header is a blank line, send a carriage return and newline character, notify the server no longer have the request header;

Request Package Body: The request package body is not used in the GET method, but is used in the Post method. The POST method is useful for situations where a customer needs to fill out a form. The most commonly used is the package body type Content-type and the package body length Content-length related to the request package body;

HTTP Response Messages

An HTTP response message consists of a status line, a response header, a blank line, and a response packet body of 4 parts, as shown in:

The following is a simple analysis of the response message format:

Status line: The status line consists of the HTTP Protocol version field, the status code, and the description text of the status Code 3 parts, separated by a space;

Response Head: The response header may include:

The Location:location response header field is used to redirect the recipient to a new location. For example: the client requested the page no longer exists in the original location, in order to redirect the client to the new location of the page, the server can send back to the address of the response header after the use of redirection statements, so that the client to access the new domain name of the corresponding server resources;

The Server:server response header field contains the software information that the server uses to process the request and its version. It corresponds to the user-agent request header domain, which sends information about the server-side software, which sends the client software (browser) and the operating system.

Vary: Indicates a list of non-cacheable request headers;

Connection: Connection mode;

For the request: Close (tells the WEB server or proxy server that after the response of this request is completed, the connection is disconnected and no subsequent requests for this connection are made). KeepAlive (Tell the Web server or proxy server, after completing the response of this request, keep the connection, wait for the subsequent request of this connection);

For the response: close (the connection is closed); KeepAlive (connection is maintained, waiting for subsequent requests for this connection); Keep-alive: If the browser requests to remain connected, the header indicates how long (in seconds) you want the Web server to remain connected; for example: keep-alive:300;

The Www-authenticate:www-authenticate response header field must be contained in a 401 (unauthorized) response message, and the header domain is related to the authorization Request header field mentioned earlier when the client receives a 401 response message, Decide whether to request the server to validate it. If the server is required to verify it, it can send a request containing the authorization header domain;

' Blank line: The last response header is followed by a blank line that sends a carriage return and a newline character, notifying the server that the following no longer has a response header.

Response Inclusion: Text information returned by the server to the client;

How HTTP Works

The HTTP protocol uses the request/response model. The client sends a request message to the server, and the server responds with a state.

The following are the steps for HTTP request/Response:

HTTP Stateless

The HTTP protocol is stateless (stateless). That is, when the same client accesses a page on the same server for the second time, the server cannot know that the client has visited and the server cannot distinguish between different clients. The stateless nature of HTTP simplifies the design of servers, making it easier for servers to support a large number of concurrent HTTP requests.

HTTP Persistent Connection

HTTP1.0 uses a non-persistent connection, the main drawback is that the client must establish and maintain a new connection for each object to be requested, that is, twice times the cost of the RTT per request for a document. Because there may be multiple objects on the same page, non-persistent connections can make a page download very slow, and this short connection increases the burden of network transmission. HTTP1.1 uses persistent connection keepalive, the so-called persistent connection, that is, the server after sending the response still for a period of time to maintain the connection, allowing multiple data requests and responses in the same connection, that is, in the case of persistent connection, the server does not close the TCP connection after sending the response, The client can continue to request other objects through this connection.

There are two ways to http/1.1 a persistent connection to a protocol:

Non-pipelined mode: The customer receives the previous response before issuing the next request;

Pipelining: The customer can then send a new request message before receiving the HTTP response message;

Finally, a specific example is given:

1234567891011121314151617181920212223242526272829 Remote Address:116.57.254.104:80   Request URL:http://hr.tencent.com/   Request Method:GET   Status Code:200 OK      Request Headers   GET / HTTP/1.1   Host: hr.tencent.com   Connection: keep-alive   Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8   User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36   Accept-Encoding: gzip,deflate,sdch   Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4   Cookie: pgv_pvi=2098703360; PHPSESSID=bc7onl0dojbsatscsfv79pds77; pgv_info=ssid=s1454606128;            pgv_pvid=926725350; ts_uid=4084753309      Response Header   HTTP/1.1 200 OK   Server: nginx   Date: Mon, 26 Jan 2015 01:09:10 GMT   Content-Type: text/html;charset=utf-8   Content-Length: 3631   Connection: keep-alive   X-Powered-By: PHP/5.3.10   Expires: Thu, 19 Nov 1981 08:52:00 GMT   Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0   Pragma: no-cache   Vary: Accept-Encoding   Content-Encoding: gzip

From the request message you can know:

1 GET / HTTP/1.1

The request method get represents a read request that will fetch the Web page data from the server, the path to the URL, the URL always starts with a/,/represents the home page, and the last http/1.1 indicates that the HTTP protocol version used is 1.1, and the request domain name is as follows:

1 Host: hr.tencent.com

The response message is as follows:

1 HTTP/1.1 200 OK   Server: nginx

Originally from: http://network.51cto.com/art/201501/464513_1.htm

HTTP response message and working principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.