study hard and make progress every day

This article has included to my lot DayDayUP:github.com/RobodLee/DayDayUP warehouse, welcome Star, more articles, please go to: directory navigation

We surf the Internet every day 🚢, many people know that the Internet is the most used HTTP protocol, but you know what is HTTP protocol? What does an HTTP packet look like? In this article, let’s take a closer look at what HTTP is.

HTTP profile

HTTP, also known as Hyper Text Transfer Protocol, is a Protocol used for communication between clients and servers. It defines the format of information to be transmitted. Those who know about computer networks should know that the TCP/IP network model is divided into four layers, from top to bottom: application layer, transport layer, network layer and network interface layer. HTTP is the uppermost application-layer protocol. When we use HTTP, when we type an address into a browser, or click on a link on a page, HTTP starts to work. It will help you to add request method, protocol type, version, the first field and a series of information constitute a complete request message sent to the server, the server will also returns a response message to you to follow the rules of the HTTP protocol, then the browser the response message parsing, presents a nice interface for you. You’re doing a very simple operation, but the HTTP protocol does a lot of the work behind the scenes to keep the communication going between you and the server.

Several concepts of HTTP

HTTP is a stateless protocol

One characteristic of the HTTP protocol is that it is stateless. Stateless means that no state is saved, meaning that HTTP itself does not save the state of communication between requests and responses. When a client sends a request to the server, the server does not remember what the last request was made.


But sometimes you need to save the status, such as when shopping taobao. So how to maintain the state function of HTTP? This uses Cookie technology. The client and server add a Cookie field in THE HTTP message to obtain the previous state information. As you can see below:


HTTP status code

What is an HTTP status code? When we visit a server, the information returned to us by the server has a number called a status code, which describes the result of the request that the server returns to the browser. Generally divided into 5 categories:

classification describe
1XX The received request is being processed
2XX The request is successfully processed
3XX Additional action is required to complete the request
4XX The server cannot process the request
5XX The server failed to process the request

It’s a lot to break down. I’ve drawn a few mind maps to make it clear:

1XX
2XX
3XX
4XX
5XX

If you want the original mind map, you can download it

I’ve already listed a lot of them, and there’s more than that, but we don’t really use them that much, so I’ve listed them here to give you a more complete idea of HTTP status codes. In fact, there are only a dozen commonly used:

Status code describe
200 OK Indicates that the request from the client is processed on the server
204 No Content Normal processing, but the response packet does not contain the body part
206 Partial Content The client makes a scope request, and the server returns only the contents of the requested scope
301 Moved Permanently Permanent redirect, indicating that the requested resource has been assigned a new URL
302 Found Temporary redirection: the requested resource is temporarily assigned a new URL, which prohibits the change from POST to GET
303 See Other The function is the same as 302, but a GET request should be used
304 Not Modified The server resource is not changed and the client’s cache is not expired
307 Temporary Redirect This is similar to 302, but does not change from POST to GET
400 Bad Request Incorrect request. Syntax errors exist in the request packet
401 Unauthorized Indicates that the sent request requires authentication information authenticated through HTTP
403 Forbidden The server rejected the browser’s request
404 Not Found I don’t have to tell you. You’ve all experienced it
500 Internal Server Error An error occurred while the server was executing the request
503 Service Unavailable The server is too busy to talk to you

HTTP method

When we send a request message to the server, we will add a method in the request line, such as GET, POST, etc. The server can know our intention through this method, so as to make a correct judgment and return the correct data. Because each method corresponds to a different intention.


Usually we use the following:

The serial number methods instructions
1 GET Requests the specified page information and returns the entity body
2 POST Submit data to the specified resource to process the request, and the data is contained in the request body. Such as login, registration
3 PUT Change the data in the server
4 HEAD Get message header
5 DELETE Deletes the specified data
6 OPTIONS Gets the HTTP request method supported by the server
7 TRACE Tracking path
8 CONNECT A tunnel protocol is required to connect the agent

The HTTP message

Had said more than a few concepts can introduce HTTP message, HTTP message is interact between client and server information, whether you sent a request to server or server is returned to you, a data is sent via the form of a message, the request is request packet, the response with the response message. Let’s take a look at an example to give an intuitive feeling, for example, let’s visit baidu’s home page:

Baidu Packet Screenshot

It can be seen that both request packets and response packets contain information such as the packet header and packet body. Of course, the request message has no body because it is a GET request.

HTTP packet Structure

The picture above is the complete message format. Let’s analyze it.

The request line contains the request method, request URI and HTTP version, such as GET /index.html HTTP/1.1, request URI may not have, for example, the above Baidu home page does not have, but you use Baidu search content will have, I will not screenshot here.

The status line contains the HTTP version, status code, and reason phrase, such as HTTP/1.1 200 OK.

A blank line is an empty line that has nothing but a header and a body separated.

There’s nothing to say about the message body, it’s just the actual data that the server gives you, or you give the server, like HTML or JSON or whatever.

The key here is the header field, which serves the purpose of passing additional important information. Is made up of the header field name and the field value, separated by a colon, like this:

Connection: keep alive

According to the actual use, it is divided into four types:

  • General header Header used by both field request and response packets.

    Generic header field
  • Request header field

    Header used to send request packets from the client to the server.

    Request header field
  • Response header field

    Header used to return response packets from the server to the client.

    Response header field
  • Entity head field

    The header used for the entity portion of request and response messages.

    Entity head field

    There is a lot of information about HTTP headers, so I won’t go into details. I got the above 4 pictures from Illustrated HTTP. If you are interested, you can have a look at this book.

The last

This is basically the end of the HTTP protocol. Related to HTTP is HTTPS, which adds a Layer of Secure Sockets Layer (SSL) to make HTTP Secure, because HTTP is a plaintext transport, which is not Secure, so I won’t go into the details here (because I don’t understand HTTPS, haha 😄). The article may have said inappropriate place, welcome message exchange.

Wechat official account