Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the HTTP request process in java?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you what the HTTP request process is like in java. I hope you will get something after reading this article. Let's discuss it together.

Preliminary knowledge

The previous article does not describe the direct hierarchical correspondence between transmission and protocol, probably supplement the corresponding protocol of data transmission in network communication, first understand the seven-layer model of OSI (Open Systems Interconnection: Open System InterConnection) and the corresponding protocols at different levels.

OSI architecture TCP/IP related protocol structure application layer HTTP,Telnet,FTP presentation layer session layer transport layer TCP,UDP network layer IP data link layer physical layer

Learn that the HTTP protocol is based on TCP connections. HTTP is a protocol that allows browsers to obtain resources from the server, which is the basis of Web. Requests are usually initiated by browsers to obtain different types of files, such as HTML files, CSS files, JavaScript files, pictures, videos, and so on. In addition, HTTP is the most widely used protocol for browsers.

If we don't know much about HTTP, we all wonder why it is faster to visit the same site than the first time, and the site is logged in after logging in once. We solve these mysteries through the analysis of the HTTP request process.

The browser initiates the HTTP request process

What steps will be completed after the browser enters the URL: http://time.geekbang.org/index.html?

1. Build request

First of all, the browser builds the request line information, and after it is built, the browser is ready to initiate a network request.

GET / index.html HTTP1.12, lookup cache

Before actually initiating a network request, the browser queries the browser cache for files to be requested. Browser caching is a technique that saves copies of resources locally for direct use on the next request.

When the browser finds that the requested resource already exists in the browser cache, it will intercept the request and return the copy of the resource to end the request. If the lookup cache fails, a network request is entered. So it will be beneficial to:

Relieve server-side pressure and improve performance

For websites, caching is an important part of realizing fast resource loading, which reduces the time to obtain resources.

3. Prepare IP address and port

We also have a general understanding of the relationship between HTTP and TCP through the preliminary knowledge of the beginning and the previous article. The browser uses the HTTP protocol as the application layer protocol to encapsulate the requested text message, and uses TCP/IP as the transport layer protocol to send it to the network, so before the HTTP work begins, the browser needs to establish a connection with the server through TCP. In other words, the content of HTTP is realized through the data transfer phase of TCP.

Diagram of the relationship between TCP and HTTP:

According to this, we can know that the establishment of HTTP network request is to parse and obtain IP and port information through URL address, and establish a server and TCP connection. We mentioned through the previous "TCP protocol" that packets are transmitted to the receiver through the IP address. The general address of our website is a domain name, so we need to map the domain name to the IP address, that is, the domain name system (DNS), which parses the IP address, resolves the IP address, and obtains the corresponding port number to obtain the precondition for establishing a connection. In other words, the browser requests DNS to return the IP corresponding to the domain name, and when requesting DNS, it will also query the DNS data cache service to determine whether the domain name has been resolved. If it has been resolved, the query will directly use it. After getting the IP, it will determine whether the URL specifies the port number. If not, the HTTP protocol defaults to port 80.

4. Wait for the TCP queue

Chrome has a mechanism that a maximum of 6 TCP connections can be established for the same domain name at the same time. If 10 requests occur under the same domain name at the same time, 4 of them will be queued until the ongoing request is completed. Of course, if the current number of requests is less than 6, you will go straight to the next step, establishing a TCP connection.

5. Establish a TCP connection

After the queue waiting ends, TCP and the server implement a "three-way handshake" (described in the previous TCP protocol), that is, the client and the server send three data packets to confirm the connection, realizing the connection between the browser and the service.

6. Send HTTP request

Once the TCP connection is established, the browser can communicate with the server. The data in HTTP is transmitted in this communication process.

HTTP request data format:

First, the browser sends a request line to the server, which includes the request method, request URI (Uniform Resource Identifier), and HTTP version protocol.

The request method is GET,POST,PUT,Delete, in which the commonly used POST will be used to send some data to the server, such as logging in to the website to send user information to the server. Generally, these data will be sent through the request body.

After the browser sends the request line command, it also sends some other information in the form of a request header to tell the server some basic information of the browser. For example, it contains information such as the operating system and browser kernel used by the browser, as well as the currently requested domain name information, Cookie and so on.

Server processing HTTP request flow 1. Return request curl-I https://time.geekbang.org/

Through the curl tool (or network panel), we can learn the format of the data returned by the server:

First, the server returns a response line, including the protocol version and the status code.

If an error occurs, the server returns the corresponding processing result by requesting the status code of the line, for example:

The most commonly used status code is 200, which indicates that the processing is successful

404, indicating that the page was not found

500, which indicates a server error

Just as the browser sends the request header with the request, the server sends the response header to the browser with the response. The response header contains some information about the server itself, such as the time when the server generates the returned data, the type of data returned (JSON, HTML, streaming media, etc.), and the Cookie to be saved by the server on the client.

After the response header, the server sends the response body data, which usually contains the actual content of the HTML. This is the process by which the server responds to the browser.

2. Disconnect

Once the server returns the requested data to the client, it closes the TCP connection. However, if the browser or server adds to its header information:

Connection:Keep-Alive

The TCP connection will remain open after it is sent, so the browser can continue to send requests over the same TCP connection. Maintaining a TCP connection can save the time of establishing a connection on the next request and speed up the loading of resources. If all the pictures embedded in a page are from the same web site, initializing a persistent connection can reuse connections that reduce TCP.

3. Redirect

The redirect returns the response line and response header:

Status 301 tells the browser that I need to redirect to another URL, and the URL that needs to be redirected is contained in the Location field of the response header. Next, the browser obtains the address in the Location field and uses that address to renavigate, which is a complete redirect process.

Summary

Through the complete process of the http request, we know that the DNS slowness and the page resource cache will be cached by the browser during the request process to reduce the resources requested from the server, so it will be faster to request the site again.

Browser resource cache processing:

As you can see from the first request in the figure above, when the server returns the HTTP response header to the browser, the browser sets whether or not to cache the resource through the Cache-Control field in the response header. Typically, we also need to set a cache expiration time for this resource, which is set by the Max-age parameter in Cache-Control.

Therefore, if the cached resource is not expired, if the resource is requested again, the cached resource will be returned directly to the browser.

If the cache expires, the browser will continue to initiate the network request and put If-None-Match in the HTTP request header. After receiving the request header, the server will determine whether the requested resource has been updated according to the value of If-None-Match.

If there is no update, the 304 status code is returned, which is equivalent to the server telling the browser that the cache can continue to be used.

If the resource is updated, the server returns the latest resource directly to the browser.

Log in to the website and submit the information to the server by POST. After the server receives the information submitted by the browser, the query verification information will generate a string of user identity on the surface and write it into the Set-Cookie field of the response header to return to the browser.

The browser parses the response header. If there is a Set-Cookie field, it is saved locally. When the user visits again, the browser will read the Cookie data and write the request hair and send it to the server before initiating the HTTP request. The server will judge the information again and display the user login status and user information if correct.

Finally, it is concluded that the HTTP request in the browser has gone through eight stages from initiation to completion: building the request, looking up the cache, preparing the IP and port, waiting for the TCP queue, establishing the TCP connection, initiating the HTTP request, the server processes the request, the server returns the request and disconnects.

Detailed HTTP request process:

After reading this article, I believe you have a certain understanding of "what the HTTP request process is like in java". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report