what does the browser do when you click a URL?
Before using PostMan, testing the Rest interface by yourself is directly tested by entering the URL in the browser address bar. However, the request sent by this method is Get. If you want to send a POST request, you can only use ajax and other programming methods. It’s much more convenient with PostMan. One day when I lost the URL, I suddenly wanted to see what the browser did, so I had the following content.
The following is an example of requesting a static web page file. The example has the following procedures:
1. Enter a URL in the browser address bar and press Enter
2. The browser uses DNS to find the IP address corresponding to the domain name.
3. The browser establishes a socket connection with the server, and if it does, it does not need to be established.
4. The browser uses the socket connection to send the request datagram.
5. The server returns a response datagram
6. Browser renders HTML
7. If the HTML contains references to other files, such as css, js files, then it will repeat steps 3~6.
Create a Socket
What information should the browser know to establish a socket connection with the server?
- Server IP address
- The application occupies the port.
How to get the IP address?
First of all, if you enter a website to access a website, you must have a domain name. If you have a domain name, you will find an IP address. Domain names and IP addresses are many-to-one relationships. It is worth mentioning that the mapping of domain names to IP addresses does not necessarily require DNS. If there is a related mapping in the local hosts file, then it is not necessary to remotely query.
Can’t I directly enter the IP address in the address bar?
As long as you remember, you can of course lose, and you have saved the trouble of resolving the domain name. The domain name appears to avoid having to remember the IP address. For example, baidu.com is better than xxx.xxx.xxx.xxx.
What about the port?
The port is directly followed by the IP address/domain name in the URL, which is generally the case:
IP address/domain name: port
For example, the tomcat project is, localost:8080/xxx
Then why do many websites do not need to enter the port when they visit?
Because it is not entered by default, it will be filled with port 80. So the port is still there.
Send request datagram
What does the request datagram contain?
That is to say the structure of the datagram, the HTTP request message is divided into three parts, the request line, the request headers, and the request body. Probably the following:
Method Uri Protocol/ version CRLF Header1: Value1 CRLF Header2: Value2 CRLF .... CRLF Body
These three blocks are separated by CRLF (CLRF is carriage return + line feed, ie “\r\n”).
Receiving response datagram
What are the responses to 200, 500, etc?
You can receive these responses, at least the network is open. To be clear, these status codes are written by the server and sent to you. The request is normally 200, the request parameter causes the service program to be abnormal, 500 or so. These status codes are many, if you are interested, you can go to the Http protocol description.
Where is the webpage data?
The content of the web file is placed in the body of the response message, and the browser renders the content of the web page to the page.
Prompt how to download the file?
This actually refers to how the response message handles the body content. By default, it is displayed directly on the web page. This is mainly determined by the Content-Disposition value in the response header. The default is inline , which is displayed directly on the web page. If it is necessary to download, it is attachement; filename=xxx. If the browser receives such a response message, a download prompt box will pop up, allowing the user to select a save location.
When is the connection disconnected?
In fact, after the Http protocol reaches 1.1, the default is to maintain a long connection, that is, it will not be closed. Because if each request establishes a new connection, the overhead of the handshake should not be underestimated. Basically, all versions are now version 1.1.
How to disconnect?
The socket connection can be disconnected as long as either party is closed, so either the browser or the server can close the connection.
Browser => Close the browser application Or prompt the server, let the server close, the specific way is to add the request header Connect:close
Server => programming call close method