HTTP Communication between a user client and a server

Like many other protocols in the TCP/IP protocol family, HTTP is used for communication between clients and servers. So the question is if one server is making interface requests to the other server between two servers who is the client? So the concept of client and server is relative. If one side plays the role of client, the other side needs to play the role of server.

Communication is achieved through the exchange of requests and responses

The HTTP protocol already stipulates that the request is sent from the client and the server responds to the request and returns the request. Let’s look at a message in a request,

GET /index.html HTTP/1.1
Host: baidu.cn
Copy the code

The GET at the beginning of the opening line indicates the type of server requested, known as method. The subsequent string index.html identifies the resource object to be accessed. Also called the request URI, and finally HTTP/1.1 is our HTTP version number, which tells clients what HTTP protocol functionality is used.

HTTP is a protocol that does not save state

HTTP is a stateless protocol that does not save state. The HTTP protocol itself does not store the state of communication between requests and responses, that is, at the HTTP level. The protocol does not persist requests or responses that have been sent.

Request the URI to locate the resource

HTTP uses URIs to locate resources on the Internet. Because of the specific function of URIs, resources can be accessed anywhere on the Internet.

HTTP method to inform the server of the intent

The GET method is used to request access to resources identified by the URI. After the specified resources are parsed by the server, the response is returned. If the request is text. Then return as is. The POST method is used to transfer the body of an entity. Although it is possible to transfer the body of an entity using the GET method, the transfer is usually done using the POST method instead of the GET method. Although POST and GET are very similar. But the primary purpose of a POST is not to get the subject content of the response. 3. PUT: Transfers files The PUT method is used to transfer files, just like uploading files through FTP. The file content is required to be included in the subject of the request message. It is then saved to the location specified by the request URI. 4. HEAD: The HEAD method is the same as the GET method, but does not return part of the packet body. Used to verify URI validity, update date, and so on. The DELET method is used to DELETE a file. It is the opposite of PUT. The DELET method asks the URI to delete the specified resource. The OPTIONS method is used to query the supported methods for the resource specified by the URI.

Persistent connections save traffic

In the original version of the HTTP protocol, a TCP connection was disconnected without an HTTP traffic. Even this was not a problem in those days, when it was all about small text transfers. With the popularity of HTTP. Documents containing a large number of images have become more common. For example, when browsing an HTML page with multiple images, the browser sends a request to access the RESOURCES on the HTML page and also requests other resources contained in the HTML page. Therefore, each request causes unnecessary TCP connection establishment and disconnection, increasing the cost of traffic. Persistent Connections To solve the problem of appealing TCP connections, HTTP/1.1 and some HTTP/1.0 came up with persistent connections, also known as HTTP keep-alive. The characteristic of a persistent connection is that the TCP connection is kept in state as long as neither end explicitly disconnects. In HTTP/1.1, all connections are persistent by default, but this is not standardized in HTTP/1.0. Although some servers implement persistent connections through non-standard means. However, persistent connections may not be supported on the server side. Of course, in addition to the server, the client also needs to support persistent connections. Piped persistent connections make it possible for most requests to be piped. After sending a request, you wait and get a response. Before sending the next request. After the advent of pipelining technology. You can send the next request directly without waiting. This enables multiple requests to be sent simultaneously and in parallel. You don’t have to wait for one response after another.

State management using cookies

We mentioned earlier that HTTP is a stateless protocol that does not manage the status of previous requests and responses. Stateless protocols certainly have their advantages. By eliminating the need to store state, the server consumes less CPU and memory resources. On the other hand, HTTP protocol itself is very simple, so it is used in a variety of scenarios.

In order to solve this problem, Cookie appears. Cookie will inform the client to save the Cookie according to a header field called set-cookie in the response packet sent from the server. When the client sends a request to the server next time, the client will automatically add Cookie to the request packet and send it out. After the server finds the Cookie sent by the client, it checks which client sent the request from, compares the records on the server, and finally obtains the previous state information.

Front-end must know must know HTTP request series (1) Understand Web and network basics



Front-end must know must HTTP request series (2) a bit simpler HTTP protocol