Network request processing method and device

文档序号:1492640 发布日期:2020-02-04 浏览:6次 中文

阅读说明:本技术 网络请求处理方法和装置 (Network request processing method and device ) 是由 赵宇 李瑜 白蓉 王方帅 熊诚伟 于 2018-07-24 设计创作,主要内容包括:本发明公开了一种网络请求处理方法和装置,涉及计算机技术领域。其中,该方法包括:在接收到APP发起的网络请求之后,根据所述网络请求携带的接口级别获取对应的请求处理策略;基于所述对应的请求处理策略对所述网络请求进行处理;在对所述网络请求处理成功的情况下,将处理后的网络请求发送至服务端。通过以上步骤,能够极大提高移动端处理各种网络请求的能力,满足不同场景下的网络请求处理需求,提高代码的复用性,提高网络请求处理的灵活性。(The invention discloses a network request processing method and device, and relates to the technical field of computers. Wherein, the method comprises the following steps: after receiving a network request initiated by an APP, acquiring a corresponding request processing strategy according to an interface level carried by the network request; processing the network request based on the corresponding request processing policy; and under the condition that the network request is successfully processed, sending the processed network request to a server. Through the steps, the capability of the mobile terminal for processing various network requests can be greatly improved, the network request processing requirements under different scenes are met, the code reusability is improved, and the flexibility of network request processing is improved.)

1. A network request processing method, the method comprising:

after receiving a network request initiated by an APP, acquiring a corresponding request processing strategy according to an interface level carried by the network request;

processing the network request based on the corresponding request processing policy;

and under the condition that the network request is successfully processed, sending the processed network request to a server.

2. The method of claim 1, further comprising:

and under the condition that the network request processing fails, sending the information of the network request processing failure to the APP in a callback mode.

3. The method of claim 1, further comprising:

after receiving a response aiming at the network request sent by the server, acquiring a corresponding response processing strategy according to an interface level carried by the network request;

processing the response based on the corresponding response processing policy;

and sending the processed response to the APP under the condition that the response processing is successful.

4. The method of claim 3, further comprising:

and under the condition that the response processing fails, sending the information of the response processing failure to the server side in a callback mode.

5. The method of claim 3, further comprising:

and configuring the interface level of the network request, and a request processing strategy and a response processing strategy corresponding to different interface levels.

6. A network request processing apparatus, the apparatus comprising:

the request processing module is used for acquiring a corresponding request processing strategy according to an interface level carried by a network request after receiving the network request initiated by the APP; further configured to process the network request based on the corresponding request processing policy;

and the sending module is used for sending the processed network request to the server side under the condition that the network request is successfully processed.

7. The apparatus of claim 6,

the sending module is further configured to send the information of network request processing failure to the APP in a callback mode when the network request processing fails.

8. The apparatus of claim 6, further comprising:

the response processing module is used for acquiring a corresponding response processing strategy according to the interface level carried by the network request after receiving a response aiming at the network request sent by the server; further for processing the response based on the corresponding response processing policy;

the sending module is further configured to send the processed response to the APP when the response is successfully processed.

9. The apparatus of claim 8,

the sending module is further configured to send the information of response processing failure to the server side in a callback manner under the condition that the response processing fails.

10. The apparatus of claim 8, further comprising:

and the configuration module is used for configuring the interface level of the network request, and the request processing strategy and the response processing strategy corresponding to different interface levels.

11. An electronic device, comprising:

one or more processors;

a storage device for storing one or more programs,

when executed by the one or more processors, cause the one or more processors to implement the method of any one of claims 1-5.

12. A computer-readable medium, on which a computer program is stored, which program, when being executed by a processor, is adapted to carry out the method of any one of claims 1 to 5.

Technical Field

The present invention relates to the field of computer technologies, and in particular, to a method and an apparatus for processing a network request.

Background

Mobile side apps (applications) almost always need to make network requests, such as requests to update apps, login requests, requests to present data, and so on. For the mobile terminal APP, an existing third-party open source library is generally used for network requests, such as okhttp3 applicable to Android APP and AFNetWorking applicable to iOS APP. Generally, we will package one layer on the basis of the open source library of the third party to meet the business needs of themselves.

For the mobile terminal APP, different network requests may need to be handled differently. For example, for a login request, it needs to be encrypted; certain data (such as city list data) is not required to be encrypted for the acquisition request. In the prior art, the mobile terminal APP often needs to write different service processing codes for different network requests.

In the process of implementing the invention, the inventor finds that at least the following problems exist in the prior art: first, in the prior art, when different network requests are processed differently, the work of analyzing data is very complicated and error is easily caused, code redundancy is serious, and multiplexing degree is low. Secondly, in the testing stage, in order to facilitate the testing, various network requests need to be subjected to plaintext (or called as 'unencrypted') communication; in the on-line stage of the project, encryption or plaintext communication needs to be performed according to different network requests, the code is greatly changed, and flexibility is lacked in switching back and forth.

Disclosure of Invention

In view of this, the present invention provides a method and an apparatus for processing a network request, which can greatly improve the capability of a mobile terminal to process various network requests, meet the network request processing requirements in different scenarios, improve code reusability, and improve flexibility of network request processing.

To achieve the above object, according to one aspect of the present invention, a network request processing method is provided.

The network request processing method of the invention comprises the following steps: after receiving a network request initiated by an APP, acquiring a corresponding request processing strategy according to an interface level carried by the network request; processing the network request based on the corresponding request processing policy; and under the condition that the network request is successfully processed, sending the processed network request to a server.

Optionally, the method further comprises: and under the condition that the network request processing fails, sending the information of the network request processing failure to the APP in a callback mode.

Optionally, the method further comprises: after receiving a response aiming at the network request sent by the server, acquiring a corresponding response processing strategy according to an interface level carried by the network request; processing the response based on the corresponding response processing policy; and sending the processed response to the APP under the condition that the response processing is successful.

Optionally, the method further comprises: and under the condition that the response processing fails, sending the information of the response processing failure to the server side in a callback mode.

Optionally, the method further comprises: and configuring the interface level of the network request, and a request processing strategy and a response processing strategy corresponding to different interface levels.

To achieve the above object, according to another aspect of the present invention, a network request processing apparatus is provided.

The network request processing device of the present invention includes: the request processing module is used for acquiring a corresponding request processing strategy according to an interface level carried by a network request after receiving the network request initiated by the APP; further configured to process the network request based on the corresponding request processing policy; and the sending module is used for sending the processed network request to the server side under the condition that the network request is successfully processed.

Optionally, the sending module is further configured to send, in the case that the network request processing fails, information of the network request processing failure to the APP in a callback manner.

Optionally, the apparatus further comprises: the response processing module is used for acquiring a corresponding response processing strategy according to the interface level carried by the network request after receiving a response aiming at the network request sent by the server; further for processing the response based on the corresponding response processing policy; the sending module is further configured to send the processed response to the APP when the response is successfully processed.

Optionally, the sending module is further configured to send, in the case that the response processing fails, information of the response processing failure to the server side in a callback manner.

Optionally, the apparatus further comprises: and the configuration module is used for configuring the interface level of the network request, and the request processing strategy and the response processing strategy corresponding to different interface levels.

To achieve the above object, according to still another aspect of the present invention, there is provided an electronic apparatus.

The electronic device of the present invention includes: one or more processors; and storage means for storing one or more programs; when the one or more programs are executed by the one or more processors, the one or more processors implement the network request processing method of the present invention.

To achieve the above object, according to still another aspect of the present invention, there is provided a computer-readable medium.

The computer-readable medium of the present invention has stored thereon a computer program which, when executed by a processor, implements the network request processing method of the present invention.

One embodiment of the above invention has the following advantages or benefits: the method comprises the steps of obtaining a corresponding request processing strategy according to an interface level carried by a network request after receiving the network request initiated by an APP, processing the network request based on the corresponding request processing strategy, and sending the processed network request to a server under the condition of successful processing, so that the capability of a mobile terminal for processing various network requests can be greatly improved, the network request processing requirements under different scenes are met, the code reusability is improved, and the flexibility of network request processing is improved.

Further effects of the above-mentioned non-conventional alternatives will be described below in connection with the embodiments.

Drawings

The drawings are included to provide a better understanding of the invention and are not to be construed as unduly limiting the invention. Wherein:

FIG. 1 is a schematic flow chart of a network request processing method according to an embodiment of the invention;

FIG. 2a is a partial flow diagram of a network request processing method according to another embodiment of the present invention;

FIG. 2b is a flow diagram of another portion of a network request processing method according to another embodiment of the present invention;

FIG. 3 is a schematic diagram of the main modules of a network request processing apparatus according to one embodiment of the present invention;

FIG. 4 is a schematic diagram of the main modules of a network request processing device according to another embodiment of the present invention;

FIG. 5 is an exemplary system architecture diagram in which embodiments of the present invention may be employed;

FIG. 6 is a schematic block diagram of a computer system suitable for use with the electronic device to implement an embodiment of the invention.

Detailed Description

Exemplary embodiments of the present invention are described below with reference to the accompanying drawings, in which various details of embodiments of the invention are included to assist understanding, and which are to be considered as merely exemplary. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the invention. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.

It should be noted that the embodiments and features of the embodiments may be combined with each other without conflict.

Before describing embodiments of the present invention in detail, some technical terms related to the present invention will be described.

Android: the operating system is a Linux-based free and open source code operating system, is mainly used for mobile equipment such as mobile phones and tablet computers, and is led and developed by Google corporation and open mobile phone alliance.

App: full Application refers to an executable Application.

A design mode: design Pattern is a set of iterative, most human-aware, categorized, code Design experience summaries. The design model enables code writing to be truly engineered, which is the fundamental context of software engineering, as is the structure of a building.

HTTP (hyper text Transfer Protocol), which is a network Protocol widely used on the internet. All WWW files must comply with this standard.

https: the full name of Hyper Text Transfer Protocol over Secure Socket Layer is an HTTP channel with security as a target, and is simply a Secure version of HTTP. Namely, an SSL layer is added under HTTP, the security base of HTTPS is SSL, and therefore SSL is needed for encryption details.

Fig. 1 is a schematic main flow diagram of a network request processing method according to an embodiment of the present invention. As shown in fig. 1, the network request processing method according to the embodiment of the present invention includes:

step S101, after receiving a network request initiated by an APP, obtaining a corresponding request processing strategy according to an interface level carried by the network request.

Wherein the network request may include a URL address, an interface level, and other request parameters. In a specific implementation, the network request may be a network request based on an http protocol, an https protocol, or another protocol.

Exemplarily, it is assumed that the interface level of the network request is divided into four levels (levels), which are respectively represented by 1, 2, 3, and 4, where the request processing policy corresponding to the interface level "1" is an unencrypted policy, the request processing policy corresponding to the interface level "2" is a simple encrypted policy, the request processing policy corresponding to the interface level "3" is a high-level encrypted policy, and the request processing policy corresponding to the interface level "4" is a compressed parameter policy.

And step S102, processing the network request based on the corresponding request processing strategy.

For example, assuming that the corresponding request processing policy is a simple encryption policy, the network request may be simply encrypted based on a specific request processing policy; assuming that the corresponding request processing policy is a high-level encryption policy, the network request may be subjected to high-level encryption processing based on the specific request processing policy.

In steps S101 and S102, the program code of the sub-flow shown may be constructed using a policy schema. A policy schema is a design schema that defines a series of algorithms and encapsulates each algorithm and makes them interchangeable. That is, the algorithm is made to vary independently of the customer using it. In the policy mode, three roles are mainly included: an environment role, an abstract policy role, a concrete policy role. Wherein the Context role holds a reference to the abstract policy (Strategy) role, also called Context; an abstract policy (Strategy) role is an abstract role, typically implemented by an interface or an abstract class; the concrete policy (ConcreteStrength) role wraps concrete algorithms and behaviors. Specifically, in the embodiment of the present invention, the sub-process shown in step S101 may be executed by an "abstract policy role", and the sub-process shown in step S102 may be executed by a "concrete policy role". Through the steps S101 and S102, when the interface level of the network request is switched in the test phase, only the corresponding request processing policy needs to be modified, and the whole network request processing flow does not need to be changed, thereby greatly improving the flexibility of network request processing.

And step S103, under the condition that the network request is successfully processed, sending the processed network request to a server.

In this step, if the network request is successfully processed, the processed network request may be directly delegated to the underlying network library, so as to be sent to the designated server. In specific implementation, the underlying network library can be an okhttp3, an AFNetWorking or other third-party network library as required.

In the embodiment of the invention, different processing can be performed on different network requests through the steps. And the mobile terminal APP can process the network request through the corresponding request processing strategy only by paying attention to the request and carrying the interface level in the request. Furthermore, the capability of the mobile terminal APP for processing various network requests is greatly improved, and the network request processing requirements under different scenes (such as a payment scene, a login scene and the like) are met; the workload required by APP developers for processing different network requests is greatly reduced, so that the APP developers can concentrate on the processing of services; the reusability of the codes is improved, so that only the corresponding request processing strategy needs to be changed when the interfaces of different levels are switched, the whole network request processing flow does not need to be changed, and the flexibility of network request processing is greatly improved.

Fig. 2a is a schematic partial flow chart of a network request processing method according to another embodiment of the present invention, and fig. 2b is a schematic partial flow chart of a network request processing method according to another embodiment of the present invention. As shown in fig. 2a and fig. 2b, the network request processing method according to the embodiment of the present invention includes:

step S201, after receiving a network request initiated by an APP, obtaining a corresponding request processing policy according to an interface level carried by the network request.

The APP may be an APP provided at a mobile terminal (e.g., a mobile phone, a tablet computer, etc.). The network request may include a URL address, an interface level, and other request parameters. In a specific implementation, the network request may be a network request based on an http protocol, an https protocol, or another protocol.

Exemplarily, it is assumed that the interface level of the network request is divided into four levels (levels), which are respectively represented by 1, 2, 3, and 4, where the request processing policy corresponding to the interface level "1" is an unencrypted policy, the request processing policy corresponding to the interface level "2" is a simple encrypted policy, the request processing policy corresponding to the interface level "3" is a high-level encrypted policy, and the request processing policy corresponding to the interface level "4" is a compressed parameter policy.

Step S202, the network request is processed based on the corresponding request processing strategy.

For example, assuming that the corresponding request processing policy is a simple encryption policy, the network request may be simply encrypted based on a specific request processing policy; assuming that the corresponding request processing policy is a high-level encryption policy, the network request may be subjected to high-level encryption processing based on the specific request processing policy.

In step S201 and step S202, the program code of the sub-flow shown may be constructed using a policy schema. A policy schema is a design schema that defines a series of algorithms and encapsulates each algorithm and makes them interchangeable. That is, the algorithm is made to vary independently of the customer using it. In the policy mode, three roles are mainly included: an environment role, an abstract policy role, a concrete policy role. Wherein the Context role holds a reference to the abstract policy (Strategy) role, also called Context; an abstract policy (Strategy) role is an abstract role, typically implemented by an interface or an abstract class; the concrete policy (ConcreteStrength) role wraps concrete algorithms and behaviors. Specifically, in the embodiment of the present invention, the sub-process shown in step S201 may be executed by an "abstract policy role", and the sub-process shown in step S202 may be executed by a "concrete policy role".

And step S203, under the condition that the network request is successfully processed, sending the processed network request to a server.

In this step, if the network request is successfully processed, the processed network request may be directly delegated to the underlying network library, so as to be sent to the designated server. In specific implementation, the underlying network library can be an okhttp3, an AFNetWorking or other third-party network library as required.

And step S204, under the condition that the network request processing fails, sending the information of the network request processing failure to the APP in a callback mode.

Step S205, after receiving the response to the network request sent by the server, obtaining a corresponding response processing policy according to the interface level carried by the network request.

18页详细技术资料下载
上一篇:一种医用注射器针头装配设备
下一篇:数据传输方法、装置及存储介质

网友询问留言

已有0条留言

还没有人留言评论。精彩留言会获得点赞!

精彩留言,会给你点赞!

技术分类