C#封装HttpClient工具类库(.NET4.5以上)

1、Json字符串实体转换扩展方法,依赖Json.Net包

/// <summary>
/// Json扩展方法
/// </summary>
public static  class JsonExtends
{
    public static T ToEntity<T>(this string val)
    {
        return JsonConvert.DeserializeObject<T>(val);
    }

    //public static List<T> ToEntityList<T>(this string val)
    //{
    //    return JsonConvert.DeserializeObject<List<T>>(val);
    //}
    public static string ToJson<T>(this T entity, Formatting formatting = Formatting.None)
    {
        return JsonConvert.SerializeObject(entity, formatting);
    }
}

2、HttpHelper封装类

public class HttpHelper
{
    /// <summary>
    /// 发起POST同步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="postData"></param>
    /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
    /// <param name="headers">填充消息头</param>
    /// <returns></returns>
    public static string HttpPost(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
    {
        postData = postData ?? "";
        using (HttpClient client = new HttpClient())
        {
            if (headers != null)
            {
                foreach (var header in headers)
                    client.DefaultRequestHeaders.Add(header.Key, header.Value);
            }
            using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
            {
                if (contentType != null)
                    httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);

                HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
                return response.Content.ReadAsStringAsync().Result;
            }
        }
    }

    /// <summary>
    /// 发起POST异步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="postData"></param>
    /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
    /// <param name="headers">填充消息头</param>
    /// <returns></returns>
    public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
    {
        postData = postData ?? "";
        using (HttpClient client = new HttpClient())
        {
            client.Timeout = new TimeSpan(0, 0, timeOut);
            if (headers != null)
            {
                foreach (var header in headers)
                    client.DefaultRequestHeaders.Add(header.Key, header.Value);
            }
            using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
            {
                if (contentType != null)
                    httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);

                HttpResponseMessage response = await client.PostAsync(url, httpContent);
                return await response.Content.ReadAsStringAsync();
            }
        }
    }

    /// <summary>
    /// 发起GET同步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="headers"></param>
    /// <param name="contentType"></param>
    /// <returns></returns>
    public static string HttpGet(string url, string contentType = "application/json", Dictionary<string, string> headers = null)
    {
        using (HttpClient client = new HttpClient())
        {
            if (contentType != null)
                client.DefaultRequestHeaders.Add("ContentType", contentType);
            if (headers != null)
            {
                foreach (var header in headers)
                    client.DefaultRequestHeaders.Add(header.Key, header.Value);
            }
            HttpResponseMessage response = client.GetAsync(url).Result;
            return response.Content.ReadAsStringAsync().Result;
        }
    }

    /// <summary>
    /// 发起GET异步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="headers"></param>
    /// <param name="contentType"></param>
    /// <returns></returns>
    public static async Task<string> HttpGetAsync(string url, string contentType = "application/json", Dictionary<string, string> headers = null)
    {
        using (HttpClient client = new HttpClient())
        {
            if (contentType != null)
                client.DefaultRequestHeaders.Add("ContentType", contentType);
            if (headers != null)
            {
                foreach (var header in headers)
                    client.DefaultRequestHeaders.Add(header.Key, header.Value);
            }
            HttpResponseMessage response = await client.GetAsync(url);
            return await response.Content.ReadAsStringAsync();
        }
    }

    /// <summary>
    /// 发起POST同步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="postData"></param>
    /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
    /// <param name="headers">填充消息头</param>
    /// <returns></returns>
    public static T HttpPost<T>(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
    {
        return HttpPost(url, postData, contentType, timeOut, headers).ToEntity<T>();
    }

    /// <summary>
    /// 发起POST异步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="postData"></param>
    /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
    /// <param name="headers">填充消息头</param>
    /// <returns></returns>
    public static async Task<T> HttpPostAsync<T>(string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
    {
        var res = await HttpPostAsync(url, postData, contentType, timeOut, headers);
        return res.ToEntity<T>();
    }

    /// <summary>
    /// 发起GET同步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="headers"></param>
    /// <param name="contentType"></param>
    /// <returns></returns>
    public static T HttpGet<T>(string url, string contentType = "application/json", Dictionary<string, string> headers = null)
    {
        return HttpGet(url, contentType, headers).ToEntity<T>();
    }

    /// <summary>
    /// 发起GET异步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="headers"></param>
    /// <param name="contentType"></param>
    /// <returns></returns>
    public static async Task<T> HttpGetAsync<T>(string url, string contentType = "application/json", Dictionary<string, string> headers = null)
    {
        var res = await HttpGetAsync(url, contentType, headers);
        return res.ToEntity<T>();
    }
}

3、辅助扩展方法

public static class HttpExtension
{
    /// <summary>
    /// 发起GET同步请求
    /// </summary>
    /// <param name="client"></param>
    /// <param name="url"></param>
    /// <param name="headers"></param>
    /// <param name="contentType"></param>
    /// <returns></returns>
    public static string HttpGet(this HttpClient client, string url, string contentType = "application/json",
                                 Dictionary<string, string> headers = null)
    {
        if (contentType != null)
            client.DefaultRequestHeaders.Add("ContentType", contentType);
        if (headers != null)
        {
            foreach (var header in headers)
                client.DefaultRequestHeaders.Add(header.Key, header.Value);
        }
        HttpResponseMessage response = client.GetAsync(url).Result;
        return response.Content.ReadAsStringAsync().Result;
    }

    /// <summary>
    /// 发起GET异步请求
    /// </summary>
    /// <param name="client"></param>
    /// <param name="url"></param>
    /// <param name="headers"></param>
    /// <param name="contentType"></param>
    /// <returns></returns>
    public static async Task<string> HttpGetAsync(this HttpClient client, string url, string contentType = "application/json", Dictionary<string, string> headers = null)
    {
        if (contentType != null)
            client.DefaultRequestHeaders.Add("ContentType", contentType);
        if (headers != null)
        {
            foreach (var header in headers)
                client.DefaultRequestHeaders.Add(header.Key, header.Value);
        }
        HttpResponseMessage response = await client.GetAsync(url);
        return await response.Content.ReadAsStringAsync();
    }

    /// <summary>
    /// 发起POST同步请求
    /// </summary>
    /// <param name="client"></param>
    /// <param name="url"></param>
    /// <param name="postData"></param>
    /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
    /// <param name="timeOut"></param>
    /// <param name="headers">填充消息头</param>
    /// <returns></returns>
    public static string HttpPost(this HttpClient client, string url, string postData = null,
                                  string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
    {
        postData = postData ?? "";
        if (headers != null)
        {
            foreach (var header in headers)
                client.DefaultRequestHeaders.Add(header.Key, header.Value);
        }
        using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
        {
            if (contentType != null)
                httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);

            HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
            return response.Content.ReadAsStringAsync().Result;
        }
    }

    /// <summary>
    /// 发起POST异步请求
    /// </summary>
    /// <param name="client"></param>
    /// <param name="url"></param>
    /// <param name="postData"></param>
    /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
    /// <param name="headers">填充消息头</param>
    /// <returns></returns>
    public static async Task<string> HttpPostAsync(this HttpClient client, string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
    {
        postData = postData ?? "";
        client.Timeout = new TimeSpan(0, 0, timeOut);
        if (headers != null)
        {
            foreach (var header in headers)
                client.DefaultRequestHeaders.Add(header.Key, header.Value);
        }
        using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
        {
            if (contentType != null)
                httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);

            HttpResponseMessage response = await client.PostAsync(url, httpContent);
            return await response.Content.ReadAsStringAsync();
        }
    }

    /// <summary>
    /// 发起POST同步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="postData"></param>
    /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
    /// <param name="headers">填充消息头</param>
    /// <returns></returns>
    public static T HttpPost<T>(this HttpClient client, string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
    {
        return client.HttpPost(url, postData, contentType, timeOut, headers).ToEntity<T>();
    }

    /// <summary>
    /// 发起POST异步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="postData"></param>
    /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
    /// <param name="headers">填充消息头</param>
    /// <returns></returns>
    public static async Task<T> HttpPostAsync<T>(this HttpClient client, string url, string postData = null, string contentType = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
    {
        var res = await client.HttpPostAsync(url, postData, contentType, timeOut, headers);
        return res.ToEntity<T>();
    }

    /// <summary>
    /// 发起GET同步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="headers"></param>
    /// <param name="contentType"></param>
    /// <returns></returns>
    public static T HttpGet<T>(this HttpClient client, string url, string contentType = "application/json", Dictionary<string, string> headers = null)
    {
        return client.HttpGet(url, contentType, headers).ToEntity<T>();
    }

    /// <summary>
    /// 发起GET异步请求
    /// </summary>
    /// <param name="url"></param>
    /// <param name="headers"></param>
    /// <param name="contentType"></param>
    /// <returns></returns>
    public static async Task<T> HttpGetAsync<T>(this HttpClient client, string url, string contentType = "application/json", Dictionary<string, string> headers = null)
    {
        var res = await client.HttpGetAsync(url, contentType, headers);
        return res.ToEntity<T>();
    }
}

 

你可能感兴趣的