HTTP response message and working principle

Source: Internet
Author: User

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:

User-agent: The type of browser that generated the request;

Accept: A list of response content types that are recognized by the client; the asterisk "*" is used to group types by scope, with "*/*" indicating that all types are acceptable, and "type/*" indicates that all subtypes of type types are acceptable;

Accept-language: Natural language acceptable to the client;

Accept-encoding: Acceptable encoding compression format for the client;

Accept-charset: An acceptable set of responses;

Host: The hostname of the request, allowing multiple domain names to be located in the same IP address, i.e., the virtual host;

Connection: Connection method (Close or keepalive);

Cookie: stored in the Client Extension field, sending a cookie belonging to that domain to the server of the same domain name;

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;

The status code consists of three digits, the first digit indicates the type of response, and the commonly used status code has five main classes as follows:

1XX: Indicates that the server has received the client request, the client can continue to send the request;

2XX: Indicates that the server has successfully received and processed the request;

3XX: Indicates that the server requires client redirection;

4XX: Indicates that the client's request has illegal content;

5XX: An unexpected error occurred when the server failed to process the client's request properly;

The status Code description text has the following values:

OK: Indicates the client request was successful;

Bad Request: Indicates that the client requests have syntax errors and cannot be understood by the server;

401 unauthonzed: Indicates that the request was not authorized and that the status code must be used with the www-authenticate header domain;

403 Forbidden: Indicates that the server received the request, but refused to provide the service, usually in the response body given the reasons for not providing services;

404 Not Found: The requested resource does not exist, for example, the wrong URL was entered;

Internal Server Error: Indicates that the server has unexpected errors, resulting in the inability to complete the client's request;

503 Service Unavailable: Indicates that the server is currently not able to process the client's request, after a period of time, the server may return to normal;

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:

The client connects to the Web server: The HTTP client establishes a TCP connection with the Web server;

The client initiates an HTTP request to the server: the client sends a request message to the server through an established TCP connection;

The server receives an HTTP request and returns an HTTP response: The server resolves the request, locates the request resource, and the server writes the resource copy to the TCP connection, which is read by the client;

Release TCP connection: If the connection mode is close, the server actively shuts down the TCP connection, the client shuts down the connection passively, releases the TCP connection, and if connection mode is keepalive, the connection remains for a period of time and can continue to receive requests ;

The client browser parses the HTML content: the client parses and displays the HTML text of the server response;

For example: Type the URL in the browser address bar and press ENTER to experience the following process:

1. The browser requests the DNS server to resolve the IP address of the domain name in the URL;

2, after resolving the IP address, according to the IP address and the default port 80, and the server to establish a TCP connection;

3, the browser issued a read file (the URL in the back part of the corresponding file) HTTP request, the request message as a TCP three handshake third message data sent to the server;

4, the server responds to the browser request, and the corresponding HTML resulting sent to the browser;

5, release the TCP connection;

6, the browser will be the HTML text and display content;

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:

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,SDC H 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 OK server:nginx date:mon, 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, No V 1981 08:52:00 GMT Cache-control:no-store, No-cache, Must-revalidate, post-check=0, pre-check=0 Pragma:no-cache V Ary:accept-encoding Content-encoding:gzip

From the request message you can know:

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:

Host:hr.tencent.com

The response message is as follows:

http/1.1 OK Server:nginx

HTTP response message and how it works (GO)

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.