.NetCore中HttpClient简单封装


HttpClient是经常用到的。我们做一个简单的封装类

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
public class HttpClientHelper
    {
        /// 
        /// application/x-www-form-urlencoded
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static async Task PostFormAsync(HttpClient _client, string uri, Dictionary<string, string> param)
        {

            try
            {
                using (var multipartFormDataContent = new FormUrlEncodedContent(param))
                {
                    Console.WriteLine(JsonConvert.SerializeObject(param));
                    var result = await _client.PostAsync(uri, multipartFormDataContent).Result.Content.ReadAsStringAsync();
                    var resp = JsonConvert.DeserializeObject(result);
                    return resp;
                }
            }
            catch (Exception ex)
            {

                throw ex;

            }

        }



        /// 
        /// application/json
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public static async Task PostAsync(HttpClient _client, string uri, dynamic requestData)
            => await SendAsync(_client, "application/json", HttpMethod.Post, uri, requestData);

        public static async Task GetAsync(HttpClient _client, string uri, Dictionary<string, string> headerDict = null)
            => await SendAsync(_client, "application/json", HttpMethod.Get, uri, null, headerDict);

        public static async Task SendAsync(HttpClient _client, string ContentType, HttpMethod method, string uri, dynamic requestData, Dictionary<string, string> headerDict=null)
        {
            string content = "";
            if (!string.IsNullOrEmpty(requestData))
            { 
                content = JsonConvert.SerializeObject(requestData, new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                });
            }
            try
            {
                using (HttpRequestMessage message = new HttpRequestMessage(method, uri))
                {
                    if(headerDict != null)
                    {
                        foreach(var d in headerDict)
                        {
                            message.Headers.Add(d.Key, d.Value);
                        } 
                    }
                    using (HttpContent httpContent = new StringContent(content, Encoding.UTF8))
                    {
                        httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse(ContentType);
                        message.Content = httpContent;

                        var httpResponseMessage = await _client.SendAsync(message);

                        string json = await httpResponseMessage.Content.ReadAsStringAsync();
                        var resp = JsonConvert.DeserializeObject(json);

                        return resp;

                    }
                }
            }
            catch (Exception ex)
            {

                throw;

            }
        } 
    }

调用方法如下:

 var client = _clientFactory.CreateClient();

            string TenantAPI = "http://xxxx"; 

            var httpRet = await HttpClientHelper.GetAsync>(client, TenantAPI);