Oss. The HTTP project's support for the. Net Standard library has been migrated, and the OSS open source family of two lowest class libraries already have the ability to support across runtimes. This article mainly contains 1. Introduction of HttpClient, 2. The idea of refactoring, 3. Easy-to-meet problems. Have a good reference value, follow the small series together to see it
Oss. The HTTP project's support for the. Net Standard library has been migrated, and the OSS open source family of two lowest class libraries already have the ability to support across runtimes. Because the Oss.http class library is a lightweight HTTP request framework that I completed a few years ago, referring to Restsharp's ideas. Because the time is the bottom of the use of HttpWebRequest, this is basically a complete reconstruction, this article mainly contains 1. Introduction of HttpClient, 2. The idea of refactoring, 3. Easy-to-meet problems.
I. Basic INTRODUCTION to HttpClient
HttpClient should be a new feature that is referenced around the. NET framework4.5 version, which was previously used by HttpWebRequest, which is more simple and clear in comparison, and most importantly fully supports the. NET Standard API, This is also the important reason that I choose it.
HttpClient has made a lot of structural adjustments, and is fully asynchronous implementation, can say from the bottom of the completion of the asynchronous support, here first introduce the corresponding several main classes:
1. Htttprequestmessage
The basic information of the request, the request address, the request action, etc., this value is in the method that the HttpClient initiates the request when the parameter is passed in, corresponds to the response httpresponsemessage
2. Httpcontent
The content body of the request, mainly contains the specific content of the request, contenttype,contentlenght, etc., is a property of Htttprequestmessage, both of which contain headers attributes, but the range is different, This is a very easy place to confuse mistakes, I gave a simple classification:
Httprequestmessage's Head (httprequestheaders) is primarily a property of the request, such as the basic properties of HTTP links such as accept,useragent,acceptencoding.
Httpcontent's Head (httpcontentheaders) is mainly the property of the current request content, mainly: Allow,content-encoding,content-length,content-type, Expires, Last-modified and so on, see the official class library.
The Httpcontent system provides several default implementations, mainly as follows:
3. Httpmessagehandler
The main role of this class is to request the definition of content processing actions, such as whether to support redirection, whether you can use cookies, proxy proxies, etc., in favor of the system settings, this value can be passed through the HttpClient constructor, The system's default provided subclass is Httpclienthandler.
4. HttpClient
The implementation of the specific request implementation, the full implementation of Post,get,delete HTTP request method, all the methods are finally called the SendAsync method.
The top four main classes, which constitute the main implementation of the HttpClient request, if you simply use, then only need to care about httpclient, as follows:
In fact, the httprequestmessage and Httpclienthandler assignments are already implemented by default within it.
Although a brief introduction, but basically can be seen, httpclient implementation has done a very clear division of labor, not again like all the previous settings are concentrated in the WebRequest. The clear most immediate advantage of the division of labor is that httpclient implements multi-request sharing, see blog:
The default HttpClient is the simplest on which you can start sending requests. A single HttpClient can is used to send as many HTTP requests as you want concurrently so in many scenarios you can just C Reate one HttpClient and then use the, for all your requests.
That is, when you want to initiate a different request in your system, you can share a httpclient, instead of having to redefine an object as Httpwebreqest basic every request, to reduce the resource consumption.
Two. Refactoring Oss.http
Back to the point, refactoring our current code module, as I said, because the. Net standard does not provide HttpWebRequest support, directly led me to make the decision to re-realize, because the previous HttpWebRequest, So I basically do a lot of packaging framework, the upper layer does not need to touch the specific underlying implementation, basically achieve the core of the RESTSHARP, interested students can refer to the code OSS. Http under Old Branch.
Before refactoring because of the httpclient is not very understanding, I would like to continue the existing framework process, transformation implementation. However, with the study of client documentation, it is found that many packages are completely unnecessary, the process has changed, so delete many of the original framework of things, re-organized the final implementation.
Of course, the implementation of HttpClient itself is simple and clear enough, but in many cases the direct invocation of Post,get and other methods will reduce the reuse of some code, Like in the Oss.social project, the bottom I just need to implement a Restcommon method to achieve global request control, the caller only need to provide url,httpmothed,parameter.
Here I draw a simple flowchart as a rendering:
The process basically does not have much access, the code on GitHub, the structure of the file is as follows:
MOS file: Enum.cs enumeration class, FileParameter.cs file parameter class, formparameter form parameter class, oshttprequest request parameter class,
OsRest.cs is the main implementation of the current encapsulation class, and in order to ensure the function of HttpClient itself, Osrest inherits from HttpClient, and provides Restsend method, In this method, the implementation of the process is completed and the SendAsync method is ultimately called to execute the request.
RestUtil.cs Auxiliary class, complete the global Osrest (HttpClient) of the common, and defined a default Httpclienthandler implementation, normal direct call to this class is OK.
The execution user customizations in the process can be set in the Requestset delegate property in Oshttprequest, for example, you can set the access type to be JSON:
Three. Easy-to-encounter problems
Although the entire refactoring code is not much, but there should still be some problems to share with you
1. Header assignment question, please see my first part, must distinguish the different headers, otherwise may give you the incorrect value error
2. You can see that there is a "get" in the flowchart above, because if it is a GET request, content is not assignable, as in Httpwebreqest, if the GET request calls the GetRequestStream method, there will be " Unable to send exception error with content body of this predicate type. Of course, if you are using oss.http as a request, then there is no problem.
3. and upload the file upload the form parameters, and separate form parameter submission, is not the same, please pay attention to processing, do not know the Osrest class can be, has done the processing.
The above is to complete the Oss.http bottom httpclient Reconstruction Package support standard library details of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!