HTTP The complete process of the request
1.1 The browser resolves according to the domain name IP Address
The browser finds the corresponding domain name IP Address .DNS The search process is as follows ：
- Browser cache ： First search the browser's own DNS cache （ The cache time is shorter , Perhaps only 1 minute , And can only hold 1000 Cache ）, Check whether there is an entry corresponding to the domain name in the cache and it has not expired , If so, this is the end of parsing , If not, proceed to 2 Step .
- System cache ： The browser searches the operating system itself DNS cache , If found and not expired, stop searching , This is the end of parsing , If not, proceed to 3 Step .
- Router cache ： Send a query request to the router , If so, this is the end of parsing , If not, proceed to 4 Step ..
- ISPDNS cache ： Inquire about ISP（ Internet service provider ） cache DNS Server for .
1.2 Browser and WEB The server creates a TCP Connect
TCP Of 3 The second handshake .
1.3 The browser gives WEB Server sends a HTTP request
One HTTP The request message is sent by the request line （request line）、 Request header （headers）、 Blank line （blank line） And request data （request body）4 Component composition .
1.3.1 Request line
The request line is divided into three parts ： Request method 、 Request address url and http Protocol version , They are separated by spaces . for example ,GET /index.html HTTP/1.1.
1. Request method
HTTP/1.1 The defined request methods are 9 Kind of （ The following table ）, The two most common get and post.（ Reference resources ：https://www.runoob.com/http/http-methods.html）
|1||GET||Request the specified page information , And return the entity body .|
|2||HEAD||Be similar to GET request , But there is no specific content in the response returned , For getting headers|
|3||POST||Submit data to the specified resource for processing request （ For example, submit a form or upload a file ）. Data is contained in the request body .POST Requests may lead to the creation of new resources and / Or modification of existing resources .|
|4||PUT||The data transmitted from the client to the server replaces the content of the specified document .|
|5||DELETE||Request the server to delete the specified page .|
|6||CONNECT||HTTP/1.1 The protocol is reserved for the proxy server that can change the connection to pipeline mode .|
|7||OPTIONS||Allow clients to view the performance of the server .|
|8||TRACE||Echo requests received by server , Mainly used for testing or diagnosis .|
|9||PATCH||It's right PUT Method supplement , Used to locally update known resources .|
When the client wants to read the document from the server , When you click the link on the web page or enter the web address in the address bar of the browser to browse the web page , All of them are GET The way .
- GET Method requires the server to url Positioning of the Resources are placed in the data part of the response message , It will be sent to the client .
- GET Use a question mark ‘？’ representative URL The end of and the beginning of the request parameters , Use... Between the parameters ‘&’ Symbols separate . for example /index.jsp?id=100&op=bind, obviously get Not suitable for transmitting private data .
- Because different browsers have different character restrictions on the address , Half can only identify 1024 Characters , So if you need to transmit a lot of data , It's not suitable for GET The way . If the data is in English letters / Numbers , The same to send ; If it's a space , Convert to +; If it's in Chinese / Other characters , You just use the string BASE64 encryption , obtain ：%E4%BD%A0%E5%A5%BD, among %XX Medium XX Is the symbol of 16 In base notation ASCII.
Allow the client to provide more information to the server .POST Method encapsulates the request parameters in HTTP Request data , By name / The form of value appears , It can transmit a lot of data , such POST There is no limit to the size of the data transmitted , And it won't show up in URL in .POST Method request line does not contain data string , These data are stored in “ Request content ” part , Data is also used between data ‘&’ Symbols separate .POST Most of the methods are used in the form of the page . because POST Also can be done GET The function of , As a result, most people use POST The way , This is a myth .GET The way has its own characteristics and advantages , We should choose to use... According to different situations GET Or use POST.
URL： Uniform resource locator , It is an abstract and unique identification method of resource location .
form ：< agreement >://< host >:< port >/< route >
Ports and paths can sometimes be omitted （HTTP The default port number is 80）
3. Protocol version
The format of the protocol version is ：HTTP/ The major version number . Sub version number , Commonly used HTTP/1.0 and HTTP/1.1
1.3.2 Request header
The request header adds some additional information to the request message , from “ name / value ” The composition of , Each row of a pair of , Use colons to separate names and values .
There will be a blank line at the end of the request header , Indicates the end of the request header , Next is the request data .
1.3.3 Request data
Request data not available GET Method used in , And in the POST Method used in .POST The method is suitable for situations where a customer is required to fill out a form . The longest used request header associated with the request data is Cntent-Type and Content-Length. Here's a POST Method ：
POST /index.php HTTP/1.1 Request line ： Request method Request address url http Protocol version Host: localhost User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2 Request header 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/ Content-Length：25 Content-Type：application/x-www-form-urlencoded Blank line username=aa&password=1234 Request data
1.4 Server response HTTP request , The browser gets HTML Code
HTTP The response message consists of the status line （status line）、 Corresponding head （headers）、 Blank line （blank line） And response data （response body）4 Component composition .
1.4.1 Status line
State line by 3 Part of it is made up of , Respectively ： Protocol version 、 Status code 、 Status code scan . The protocol version is consistent with the request message , State code description is a simple description of state code .
|1**||Information , The server receives the request , Requester is required to continue|
|2**||success , Operation received and processed successfully|
|3**||Redirect , Further action is required to complete the request|
|4**||Client error , The request contains a syntax error or could not be completed|
|5**||Server error , The server encountered an error while processing the request|
Reference resources ：https://www.runoob.com/http/http-status-codes.html
1.4.2 Response head
1.4.3 The response data
It is used to store the data information that needs to be returned to the client .
HTTP/1.1 200 OK Status line Date: Sun, 17 Mar 2013 08:12:54 GMT Response head 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 Blank line <html> The response data <head> <title>HTTP Response example <title> </head> <body> Hello HTTP! </body> </html>
1.5 Browser parsing HTML Code , And ask for HTML Resources in the code
The browser gets HTML After the document , Start parsing HTML Code , When a static resource is encountered , Just ask the server to download .
1.6 close TCP Connect , The browser renders the page to the user
The browser uses its own internal working mechanism , Put the requested static resource and HTML Code rendering , Present to the user .