Current location - Quotes Website - Signature design - Summary of basic knowledge of App network
Summary of basic knowledge of App network

The network module is the most basic and core module of the App application. Stable and efficient network processing is the basic guarantee for a good user experience. This article introduces network protocols commonly used in daily development and how to use them.

Let’s first take a look at the process of network request transmission:

The core steps are mainly the following steps:

The Http protocol is divided into two parts: Request and Response. The client initiates a Request; after processing, the server returns a Response.

Take a look at the structure of Request:

The upper part is the request header, and the lower part is the request body.

The first line describes the request method, the request path, and the http protocol version. Each subsequent line describes a request header field. The request header and request body are separated by two newlines.

Commonly used request header fields:

Response code: Whether the request is successful is judged based on the response code. Each response code corresponds to its own meaning. For details, refer to wiki

Commonly used response headers:

Cookie is the key-value pair data returned with the Response, which is stored locally. The next time you visit the same domain, the cookie's kvs will be brought to the server along with the request. Backend developers can set corresponding cookies according to business needs.

For App scenarios, using the http protocol to handle network requests is the simplest, but not the most efficient way (personal opinion)

As shown in the figure above, the consumption of Http requests It mainly consists of four parts. Analyze optimization strategies around these four parts.

The fastest request is to make no request. Not only does network caching need to be handled at the protocol layer, but data caching also needs to be handled at the business layer. When unnecessary, try not to initiate network requests and use the data in the cache directly.

The system has the implementation of Dns resolution. By default, the system API is directly called to perform Dns query operations.

http 1.1 defaults to setting Connection:Keep-Alive. This means that the request will not be closed immediately after the request is completed. Subsequent identical requests will be reused through the link, saving the time required to establish the TCP link.

When the bandwidth is fixed, reduce the size of submitted data packets and reduce the time of data transmission.

Relatively speaking, the formats of json and urlEncode take up less space. formdata is relatively large and is generally used when uploading files.

The principle is the same as above, reducing the size of data packets and reducing the time of data transmission.

There are two aspects to the security handling strategy. One is to add security verification in use, and the other is to use security protocols in protocols, such as HTTPS.

A common practice for front-end and back-end interaction. Certain values ??of the requested parameters are concatenated together, and then a signature is generated by adding salt to the MD5 value. Append the signature value to the request parameters. The backend also needs to verify the signature after receiving the request. If it is inconsistent, it means that the request parameters have been tampered with and will not be passed.

A slightly more strict method is to naturally sort all request parameters according to the key value, and then use the above method to calculate the signature again.

This method is mainly to prevent parameters from being tampered with, but it does not prevent interception.

Using HTTPS can solve security problems from the protocol. Comprehensive upgrade to HTTPS is the current trend in the industry. The following figure is a simplified diagram of the HTTPS processing process:

The HTTP protocol is a passive way of processing messages. In many app scenarios, the server needs to actively push data to the client. Using the WS protocol to maintain long client-to-server connections is a good solution. Currently it is widely used in IM or live broadcast scenarios.

The above two pictures show the ws protocol request and response.

Let’s take a look at the data composition of each frame under the ws protocol:

Each frame of data contains 2 bytes of header information.

FIN indicates whether it is the last frame.

RSV1/2/3 is an extension field. The client and server can implement additional operations on the protocol by agreeing on the values ??of these fields, such as whether to enable data compression.

OP Code is the operation type of each frame. For example, whether the current frame is an operation frame or a data frame

MASK 0/1 indicates whether the mask is set

LENGTH is the data packet length

after the 2byte header information Bring the real data of the entire frame.

The next generation of http protocol. It solves many problems under http and is used more and more widely. My summary is not very good. Please refer to another blog for details about HTTP 2.0.

The network scenario of App is much more complicated and unstable than that of PC. For apps, the ideal network model characteristics should have:

Use different tools to solve problems in different scenarios. The key point is to be familiar with the characteristics of each protocol and how to use it.

Advertisement: Network tool library BJNetwork based on OkHttp