preface

Are you curious how the HTTP protocol you learned earlier, the seven/four layer network model, and a network request process in business code relate? Are you curious about the difference between a frame-encapsulated HTTP request and a native one? If you’re interested, take a look. Note: As external links are banned on wechat platform, all references are at the end of the article.

Seven layer/four layer network model

What is the use of the seven/four tier model? Actually, it doesn’t help. Oh, no, not for the average Internet product developer. It is the interpretation of a network request data transmission link, code development, no special requirements, is not to operate these. The complete network protocol map for 2020 can be found at Network Communication Protocol-2020.pdf

Taking the four-tier model as an example, the fourth layer above (application layer) is the layer where developers write interface operations. When the interface function of the network request is executed, it will confirm the transmission protocol, distribute routes and address the following layer 1 and 2 protocols, and then reach the link layer below, and then the link layer realizes the transmission of data.

HTTP & TCP

Many people confuse HTTP with TCP. In fact, the HTTP protocol is a network messaging specification. For example, if two people want to communicate, we should first agree whether to speak Chinese or English. If one only knows Chinese and one only knows English, how can one communicate? Message transfer between two computers is the same. If the protocol is HTTP, both use this protocol, so that a message sent by one computer can be interpreted by the other. As for why HTTP is used, and not some other protocol, of course, when the Internet was born, HTTP was proposed, and people didn’t bother to change to other protocols, so if everyone uses this protocol, I’m going to use this protocol. However, due to the evolution of enterprise architecture system in recent years, RPC protocol, DuBBO protocol and so on have emerged.

TCP is a messaging protocol. It specifies how the HTTP encapsulated network message is transmitted. For example, after two people agree to communicate in Chinese, which way to choose to communicate, face to face, phone or letter. TCP is responsible for this transmission process, so TCP protocol belongs to the transport layer.

Therefore, HTTP and TCP do not compete, one belongs to the application layer, the other belongs to the transport layer. They are often mentioned together because they are the most commonly used agreements, the best partners. Like Liu Guoliang and Zhang Jike, but on different tracks, hahaha.

Linux Network Request

Because C/C++ does not have HTTP communication facilities, the libcurl component library is usually installed to enable Linux to make network requests.

Curl Installation process:

1. Use the Linux system installation command

  yum install curl 
Copy the code

2. Compile the source code package for installation

Curl curl curl curl curl curl curl curl curl curl curl curlCopy the code

So how does the curl command initiate this web request?

You can check out the curl source code for an analysis of how to implement curl.

If you don’t want to see complicated code, you can check out this short version of a blog: IMPLEMENTING HTTP requests in C. In addition to strcat (string concatenation), several key C functions are used to implement an HTTP request: inet_pton (IP address translation function), Socket, and connect.

Strcat encapsulates HTTP messages, including the request header, domain name, port, route, message content, and message length. For details about request packets and response packets, see HTTP/HTTPS request packets and response packets.

One of the parameters identifies whether TCP or UDP is used for transmission, which is the transport layer protocol Settings mentioned above. When the TCP protocol is set up, the connect function triggers the connection between the computer and the remote server. The connection process goes through the common three-way handshake.

At this point, we have introduced how HTTP and TCP are implemented in a network request.

Network data transmission process

After the message is encapsulated in HTTP format and TCP is selected as the transport protocol, the computer invokes the network layer device (router) to address the route, and then sends the data through the data link layer.

This process is analyzed from the perspective of code operation. The overall process is that high-level language (business code logic, that is, the message processing logic mentioned above) is compiled into assembly language, which executes CPU instructions, and CPU instructions control the computer. The computer’s digital signals (bytecode) are then converted by transistors into analog signals (current information), which are transmitted over a transmission line. After reaching the other party’s computer, the process is reverse-executed to retrieve the requested data.

The CPU executes the instruction set, and the conversion between digital and analog signals is explained here: how the CPU instruction set executes.

Java native network request

The JAVa.net package of the JDK provides the basic function class HttpURLConnection for accessing the HTTP protocol. Java initiates Http requests. From the main function logic, it can be seen that Java initiates network requests after setting network request parameters: This includes information such as Host, as well as the encapsulation of the message body (hence HTTP and TCP protocols are referred to as basic principles of computer composition, and different language stacks writing business code are just different types of implementations of the principles. It’s like buying Ippo and Nongfu Spring. They are both water, but the brands are different. Hence the expression “language is just an implementation tool”). Specific message sending underlying principle is also sent through socket, the relevant source analysis can see here: HttpURLConnection source analysis.

Therefore, no matter what language initiates the HTTP network request, it needs to follow the MESSAGE encapsulation format of THE HTTP protocol specification, select transmission protocols such as TCP or UDP, and call socket to send messages. The socket function can realize communication because the computer finally compiles the high-level programming language into assembly language, assembly language executes the machine command (CPU instruction set, etc.), controls the memory unit, logical processing unit, and peripheral circuit to work. And peripheral circuit of the transistor will machine instruction digital signals into analog signals, form the transmission current in the circuit, when the analog signal transmitted to another news receiving equipment, then reverse the process, thus the target machine can get the current computer send messages, to implement a network communication.

Spring Framework network requests

The framework is the encapsulation of the underlying functions to improve the development efficiency. Java’s native class library encapsulates socket functions to implement network requests. But it’s not particularly elegant to use, and the code is redundant.

In order to facilitate the use of developers, and reduce the redundancy rate of code, the Spring framework of the client network request function is also encapsulated in Java basic functions. In addition to httpUrlConnection, There are also httpClient, SpringBoot-RestTemplate, closeableHttpClient, Feign, openFeign. See blog: Common Ways to initiate Http Requests in Java for more details.

This is the first draft, and the details will be supplemented and improved from time to time

References:

1, network communication protocol – 2020. PDF: www.colasoft.com.cn/download/ne… 2, the curl source code to compile installation: blog.csdn.net/peng3148995… 3, libcurl source code analysis: www.cnblogs.com/lidabo/p/45… 4, C language implementation HTTP request: www.jianshu.com/p/867632980… 5, HTTP/HTTPS request and response message message: www.jianshu.com/p/64a64a049… 6, sockets functions: baike.baidu.com search socket function 7, the TCP three-way handshake a: zhuanlan.zhihu.com/p/40013850 8, CPU instruction set is how to perform: Zhidao.baidu.com/question/23… 9, Java to initiate Http request: blog.csdn.net/qq_40036754… 10, HttpURLConnection source code analysis: blog.csdn.net/Charon_Chui… 11. A common way to initiate Http requests in Java is blog.csdn.net/riemann_/ar…

Transfer from wechat public number [program yuan’s bed] : mp.weixin.qq.com/s?__biz=Mzk…