Understanding the HTTP request process again
I have read the http protocol for many times, but I still don't understand it deeply. Here, I will read it again and try again. Deepen two points: parse the header information \ r \ n, and use \ r \ n to separate the header and the subject. The reason why a single request will see a lot of requests in the network is that the browser has accessed it multiple times instead! The program only processes it once!
HTTP Request
Step 1: The browser first sends an HTTP request to the server, including:
Method: GET or POST. GET only requests resources. POST will include user data;
Path:/html/path;
Domain Name: specified by the Host Header: Host: www.sina.com.cn
And Other Related headers;
If it is POST, The request also contains a Body containing user data.
Step 2: The server returns an HTTP Response to the browser. The response includes:
Response Code: 200 indicates success, 3xx indicates redirection, 4xx indicates an error occurred in the request sent by the client, and 5xx indicates an error occurred during server processing;
Response Type: specified by Content-Type;
And Other Related headers;
Generally, the HTTP Response of the server carries content, that is, a Body containing the response content. The HTML source code of the webpage is in the Body.
Step 3: If the browser still needs to request other resources from the server, the browser sends an HTTP request again, repeat steps 1 and 2.
The HTTP protocol used by the Web adopts a very simple request-response mode, which greatly simplifies development. When writing a page, we only need to send HTML in the HTTP request, without considering how to attach images, videos, etc. If the browser needs to request images and videos, it sends another HTTP request. Therefore, an HTTP request only processes one resource.
HTTP protocol is highly scalable at the same time, although the browser requestshttp://www.sina.com.cn/
But Sina can link resources of other servers in HTML, such
In this way, the request pressure is distributed to each server, and a site can be linked to other sites. countless sites are linked to each other, forming the World Wide Web, WWW for short.
HTTP format
Each HTTP request and response follow the same format. An HTTP request contains the Header and Body, and the Body is optional.
HTTP is a text protocol, so its format is also very simple. Http get request format:
GET /path HTTP/1.1Header1: Value1Header2: Value2
Each Header has one row, and the line break is\r\n
.
Http post request format:
POST /path HTTP/1.1Header1: Value1Header2: Value2body data goes here...
When two consecutive\r\n
The Header part ends, and all the subsequent data is Body.
HTTP Response format:
200 OKHeader1: Value1Header2: Value2
body data goes here...
If the HTTP response contains the body\r\n\r\n
. Please note that the data type of the Body isContent-Type
Header to determine, if it is a web page, the Body is text, if it is an image, the Body is the binary data of the image.
IfContent-Encoding
The Body data is compressed, and the most common compression method is gzip.Content-Encoding: gzip
To obtain the real data, extract the Body data first. The purpose of compression is to reduce the Body size and speed up network transmission.