Browser HTTP request analysis

Overview

Ordinary netizens do not need to understand the so-called HTTP protocol to open web pages and visit websites. As a software engineer, it is quite interesting to understand the working process of the browser. Let me introduce to you how the browser communicates with the web server when we press Enter in the address bar of the browser.

DNS domain name resolution

The first step the browser does is DNS domain name resolution. The main function of domain name resolution is to convert domain names into IP addresses. For example, it is the domain name, and 202.108.33.81 is the IP address. Windows has a command called nslookup, which is a domain name resolution command.


Through nslookup, we found that Sina's domain name resolves to many IP addresses, and Sina's Web site is large, so there are many Web servers, and multiple Web servers are used to solve the load problem.

Establish Socket connection

The second step is to establish a Socket connection with the Web server. The web server defaults to port 80. We enter http: // in the address bar and the browser connects to port 80 of the 202.108.33.81 server. If the web server is not port 80, for example port 8080, we need to enter http: //: 8080.

HTTP request and response

The third step is that the browser sends an HTTP request to the Web server through Socket and receives an HTTP response. The HTTP protocol is more complicated. The following is an example of an HTT request:

Host: rss.sina.com.cn

User-Agent: Mozilla / 5.0 (Windows; U; Windows NT 5.1; zh-CN; rv: 1.8.1.14) Gecko / 20080404 Firefox / 2.0.0.14

Accept: text / xml, applicaTIon / xml, applicaTIon / xhtml + xml, text / html; q = 0.9, text / plain; q = 0.8, image / png, * / *; q = 0.5

Accept-Language: zh-cn, zh; q = 0.5

Accept-Encoding: gzip, deflate

Accept-Charset: gb2312, utf-8; q = 0.7, *; q = 0.7

Keep-Alive: 300

ConnecTIon: keep-alive

Cookie: userId = C5bYpXrimdmsiQmsBPnE1Vn8ZQmdWSm3WRlEB3vRwTnRtW <-Cookie

If-Modified-Since: Sun, 01 Jun 2008 12:05:30 GMT

Cache-Control: max-age = 0

Example of HTTP response message header:

Status: OK-200

Date: Sun, 01 Jun 2008 12:35:47 GMT

Server: Apache / 2.0.61 (Unix)

Last-Modified: Sun, 01 Jun 2008 12:35:30 GMT

Accept-Ranges: bytes

Content-Length: 18616

Cache-Control: max-age = 120

Expires: Sun, 01 Jun 2008 12:37:47 GMT

Content-Type: applicaTIon / xml

Age: 2

Connection: close

Download web text content

Step 4: The browser downloads the webpage text content, and pictures, CSS files, Flash, etc. are marked in the webpage text. After the browser analyzes the content, it opens four threads to download the content and displays it on the screen.

Power bank

Dongguan Guancheng Precision Plastic Manufacturing Co., Ltd. , https://www.dpowerchargers.com