A technically open-source dream-Http request client based on public components of. Net Core,. netcore

Source: Internet
Author: User

A technically open-source dream-Http request client based on public components of. Net Core,. netcore

An open-source dream of technology-directory

Presumably, during project development, everyone should have called their internal interfaces in the program or used interfaces provided by third parties. Today we will not discuss REST, the most common requests should be GET and POST. The following describes the simple encapsulation of the Http request client.

First, the good news is. net Core has replaced the previous System. net. the Http component is added to NETStandard by default. library, so you can use it directly. You don't need to install it on Nuget any more. It is said that Nuget will have an article about Nuget package generation and uploading and publishing in the future, it will be sorted out and released in the near future.

Name the Http request ClientHttpReqeustClientTemporary internal useHttpClient(In both the System. Net. Http component). Currently, the following functions are implemented.

  • Send a GET request based on the url to obtain the response text;
  • Sends a GET request based on the url to obtain the binary array of the response. (used for File Download scenarios)
  • Send a POST request based on the url to obtain the response text.
    • Dictionary <string, string> postData parameter;
    • HttpPostDataDictionary postData parameter. (Common text and file types can be specified)
    • In the future, the SDK will add the Object parameter and directly serialize it into a JSON string for submission;
  • Add Http request headers, add Cookies, and add certificates used by requests.

HttpPostDataTypeSpecifies whether the submitted data is text or file.

1 namespace Wlitsoft. Framework. Common. Net 2 {3 /// <summary> 4 // Http submit data type. 5 /// </summary> 6 public enum HttpPostDataType 7 {8 /// <summary> 9 // text. 10 /// </summary> 11 Text, 12 13 /// <summary> 14 /// file path. 15 /// </summary> 16 FilePath, 17 18 /// <summary> 19 /// file stream. 20 /// </summary> 21 FileStream22} 23}View Code

HttpPostDataDictionaryHttp submits a data dictionary. The data dictionary to be submitted contains common text or file types.

1 using System. collections. generic; 2 using System. IO; 3 using System. net. http; 4 using Wlitsoft. framework. common. exception; 5 6 namespace Wlitsoft. framework. common. net 7 {8 // <summary> 9 // submit the data dictionary over Http. 10 /// </summary> 11 public class HttpPostDataDictionary: Dictionary <string, KeyValuePair <HttpPostDataType, object> 12 {13 14 /// <summary> 15 // Add text data. 16 /// </summary> 17 // <param name = "name"> HTTP Content name. </Param> 18 // <param name = "value"> text value. </Param> 19 public void AddText (string name, string value) 20 {21 # region parameter verification 22 23 if (string. isNullOrEmpty (name) 24 throw new StringNullOrEmptyException (nameof (name); 25 26 if (string. isNullOrEmpty (value) 27 throw new StringNullOrEmptyException (nameof (value); 28 29 # endregion30 31 this. add (name, new KeyValuePair <HttpPostDataType, object> (HttpPostDataType. text, value); 32} 33 34 // <summary> 35 /// Add file data. 36 /// </summary> 37 // <param name = "name"> HTTP Content name. </Param> 38 // <param name = "filePath"> file path. </Param> 39 public void AddFile (string name, string filePath) 40 {41 # region parameter verification 42 43 if (string. isNullOrEmpty (name) 44 throw new StringNullOrEmptyException (nameof (name); 45 46 if (string. isNullOrEmpty (filePath) 47 throw new StringNullOrEmptyException (nameof (filePath); 48 49 # endregion50 51 throw new System. notImplementedException (); 52} 53 54 // <summary> 55 // Add a file stream. 56 /// </summary> 57 // <param name = "name"> HTTP Content name. </Param> 58 // <param name = "fileStream"> file stream. </Param> 59 public void AddFile (string name, FileStream fileStream) 60 {61 # region parameter verification 62 63 if (string. isNullOrEmpty (name) 64 throw new StringNullOrEmptyException (nameof (name); 65 66 if (fileStream = null) 67 throw new ObjectNullException (nameof (fileStream )); 68 69 # endregion70 71 this. add (name, new KeyValuePair <HttpPostDataType, object> (HttpPostDataType. fileStream, fileStream); 72} 73} 74}View Code

It provides public methods such as adding text and adding files.

Okay, the most importantHttpReqeustClientClass

Public attributes

/// <Summary> /// obtain the <see cref = "HttpClient"/> instance used by the current request. /// </Summary> public HttpClient {get; private set ;}/// <summary> /// obtain the HTTP message containing the status code and data. /// </Summary> public HttpResponseMessage {get; private set ;}/// <summary> /// obtain the Http Request Header set. /// </Summary> public Dictionary <string, string> Headers {get ;}/// <summary> /// obtain or set the Cookie set container. /// </Summary> public CookieContainer {get; set ;}/// <summary> /// obtain or set the security certificate to be used. /// </Summary> public X509Certificate Certificate {get; set ;}

Method Signature

/// <Summary> /// send a GET request to obtain the response text based on <paramref name = "url"/>. /// </Summary> /// <param name = "url"> the url to be requested. </Param> // <returns> the response text of the server. </Returns> public string HttpGetString (string url) /// <summary> /// obtain the binary array of the response by sending a GET request according to <paramref name = "url"/>. /// </Summary> /// <param name = "url"> the url to be requested. </Param> /// <returns> the binary array of the server response. </Returns> public byte [] HttpGetBytes (string url) /// <summary> /// send a POST request to obtain the response text based on <paramref name = "url"/>. /// </Summary> /// <param name = "url"> the url to be requested. </Param> /// <param name = "postData"> the data to be sent. </Param> // <returns> the response text of the server. </Returns> public string HttpPost (string url, Dictionary <string, string> postData) /// <summary> /// send a POST request to obtain the response text based on <paramref name = "url"/>. /// </Summary> /// <param name = "url"> the url to be requested. </Param> /// <param name = "postData"> the data to be sent. </Param> // <returns> the response text of the server. </Returns> public string HttpPost (string url, HttpPostDataDictionary postData) /// <summary> /// send a POST request to obtain the response text based on <paramref name = "url"/>. /// </Summary> /// <param name = "url"> the url to be requested. </Param> /// <param name = "httpContent"> HTTP Object Body object. </Param> // <returns> the response text of the server. </Returns> public string HttpPost (string url, HttpContent httpContent)

 

An open-source dream of technology-directory

To be continued.

Next article: A technology Wang's open-source dream-string encryption and Http Request Parameter Signature Based on. Net Core public components

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.