HTTP request connection process

Whether it is daily web development or preparing interview questions, the http request process is a knowledge point that must be understood. In this article, we will learn and understand the request process of the HTTP communication mechanism.


As we all know, the HTTP protocol is a web communication protocol based on the TCP network connection protocol. It has "four based" characteristics:

Request and response : the client sends a request, and the server responds with data

Stateless : The protocol has no memory for transaction processing. When the client establishes a connection with the server for the first time and sends a request, it needs to perform a series of security authentication matching, etc., so the page waiting time is increased. When the client sends a request to the server, the server After the client completes the response, the two disconnect and do not save the connection status, which is like putting up their pants and not recognizing the person. The next time the client sends a request to the same server, because they have forgotten each other before, they need to Re-establish the connection.

Application layer : Http is a protocol belonging to the application layer, which is used in conjunction with TCP/IP.

TCP/IP : Http uses TCP as its supporting transport protocol. The HTTP client initiates a TCP connection with the server. Once the connection is established, the browser (client) and server processes can access TCP through the socket interface.

request process

The HTTP request process can be intuitively shown in the figure below:
Insert image description here
It can be seen that in a complete HTTP communication process, the following steps will be completed between the web browser and the web server:

1. Domain name resolution

2. Initiate TCP 3-way handshake

3. The web browser sends an http request message to the web server

Once a TCP connection is established, the web browser sends a request command to the web server. For example: GET/sample/hello.jsp HTTP/1.1.
An HTTP request message consists of four parts: request line, request headers, blank line and request body.
Insert image description here

3.1 Request line

The request line is divided into three parts: request method, request address URL and HTTP protocol version, which are separated by spaces. For example, GET /index.html HTTP/1.1.

3.2 Request method

There are 8 request methods defined by HTTP/1.1: GET (complete request for a resource), POST (submit form), PUT (upload file), DELETE (delete), PATCH, HEAD (request response header only), OPTIONS (return request) Methods supported by resources), TRACE (pursuing the proxy passed by a resource request). The two most common ones are GET and POST. If it is a RESTful interface, GET, POST, DELETE, and PUT are generally used.

3.3 Request data

The request data is not used in the GET method, but in the POST method. The POST method is suitable for situations where customers are required to fill out a form. The most commonly used request headers related to request data are Cntent-Type and Content-Length.

4. The web browser sends http request header information

After the browser sends its request command, it also sends some other information to the Web server in the form of header information. Then the browser sends a blank line to notify the server that it has finished sending the header information.

Request message example:

POST  /index.php HTTP/1.1    请求行

Host: localhost

User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2  请求头

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8

Accept-Language: zh-cn,zh;q=0.5

Accept-Encoding: gzip, deflate

Connection: keep-alive

Referer: http://localhost/




username=aa&password=1234  请求数据

5. Web server response

After the browser sends a request to the server, the server sends a response back to the browser HTTP/1.1 200 OK. The first part of the response is the protocol version number and the response status code. The HTTP response message consists of four parts: status line, corresponding headers, blank line and response body.
Insert image description here

6. The web server sends response header information

Just as the client sends information about itself with the request, the server sends data about itself and the requested document to the user with the response.
Insert image description here

7. Web server sends data to browser

After the web server sends the header information to the browser, it sends a blank line to indicate that the sending of the header information has ended. Then, it Content-Typesends the actual data requested by the user in the format described by the response header information.

Response message example:

HTTP/1.1 200 OK  状态行

Date: Sun, 17 Mar 2013 08:12:54 GMT  响应头部

Server: Apache/2.2.8 (Win32) PHP/5.2.5

X-Powered-By: PHP/5.2.5

Set-Cookie: PHPSESSID=c0huq7pdkmm5gg6osoe3mgjmm3; path=/

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

Content-Length: 4393

Keep-Alive: timeout=5, max=100

Connection: Keep-Alive

Content-Type: text/html; charset=utf-8


<html>  响应数据





Hello HTTP!



8. Web server closes TCP connection

Normally, once the web server sends the request data to the browser, it will close the TCP connection. Then if the browser or server adds this line of code in its header information: , the TCP connection will remain open after sending it Connection:keep-alive , The browser can then continue to send requests over the same connection. Keeping connections saves the time required to establish a new connection for each request and also saves network bandwidth.

Case Analysis

For example, enter in the address bar of the browser, and then press Enter. What do the browser and server do after pressing Enter? The process is roughly as follows:
domain name resolution --> initiate TCP 3-way handshake --> initiate http request after establishing TCP connection --> server responds to http request, browser gets html code --> browser parses html code and requests html Resources in the code (such as js, css, pictures, etc.) --> The browser renders the page and presents it to the user

For a more detailed analysis and explanation, please refer to:
A complete HTTP request process
The complete process of an HTTP request

Guess you like