Introduction to HTTP Protocol

HTTP request

Step 1: The browser first sends an HTTP request to the server, the request includes:

Method: GET or POST, GET only requests resources, POST will attach user data;

path: /full/url/path;

Domain name: specified by the Host header: Host: www.sina.com.cn

and other related Header;

If it is a POST, the request also includes a Body, which contains user data.

Step 2: The server returns an HTTP response to the browser, and the response includes:

Response code: 200 means success, 3xx means redirection, 4xx means there is an error in the request sent by the client, 5xx means an error occurred in the server-side processing;

Response type: specified by Content-Type;

and other related Header;

Usually the HTTP response of the server will carry the content, that is, there is a Body, which contains the content of the response, and the HTML source code of the web page is in the Body.

Step 3: If the browser still needs to continue to request other resources, such as pictures, from the server, it sends an HTTP request again, and repeats steps 1 and 2.

The HTTP protocol adopted by the Web uses a very simple request-response model, which greatly simplifies development. When we write a page, we only need to send HTML in the HTTP request, no need to consider how to attach pictures, videos, etc. If the browser needs to request pictures and videos, it will send another HTTP request, so an HTTP request The request handles only one resource.

HTTP format

Format of HTTP GET request:

GET /path HTTP/1.1
Header1: Value1
Header2: Value2
Header3: Value3

Each header is one per line, and the newline character is \r\n.

Format of HTTP POST request:

POST /path HTTP/1.1
Header1: Value1
Header2: Value2
Header3: Value3

body data goes here...

When two consecutive \r\n are encountered, the Header part ends, and the following data is all Body.

Format of the HTTP response:

200 OK
Header1: Value1
Header2: Value2
Header3: Value3

body data goes here...

If the HTTP response contains a body, it is also separated by \r\n\r\n. Please note again that the data type of the Body is determined by the Content-Type header. If it is a web page, the Body is the text, and if it is an image, the Body is the binary data of the image.

当存在Content-Encoding时,Body数据是被压缩的,最常见的压缩方式是gzip,所以,看到Content-Encoding: gzip时,需要将Body数据先解压缩,才能得到真正的数据。压缩的目的在于减少Body的大小,加快网络传输。


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325739230&siteId=291194637