在写C#客户端程序时,或者在服务之间调用API时,我们往往会用到HttpClient来进行交互,这里我做了下简单的二次封装,并不定期更新。
下面是整个封装的HttpClient帮助类:
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Text;
using System.Threading.Tasks;
namespace Common
{
/// <summary>
/// Summary: HttpClient公共类
/// Author: Lee Liu
/// Date: 20190114
/// </summary>
public class HttpClientHelper
{
private static HttpClientHelper httpClientHelper = null;
private HttpClient httpClient;
/// <summary>
/// 构造方法私有,用于单例
/// </summary>
private HttpClientHelper() { }
/// <summary>
/// 获取当前类的实例
/// </summary>
/// <returns></returns>
public static HttpClientHelper GetInstance()
{
if (httpClientHelper != null)
{
return httpClientHelper;
}
else
{
HttpClientHelper httpClientHelper = new HttpClientHelper();
//取消使用默认的Cookies
HttpClientHandler handler = new HttpClientHandler() { UseCookies = false };
httpClientHelper.httpClient = new HttpClient(handler);
return httpClientHelper;
}
}
/// <summary>
/// Get方法请求
/// </summary>
/// <param name="url">请求地址</param>
/// <returns></returns>
public HttpResponseMessage Get(string url, List<KeyValuePair<string, string>> headers = null)
{
HttpRequestMessage request = new HttpRequestMessage()
{
RequestUri = new Uri(url),
Method = HttpMethod.Get,
};
if (headers != null && headers.Count > 0)
{
request.Headers.Clear();
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}
HttpResponseMessage response = httpClient.SendAsync(request).Result;
return response;
}
/// <summary>
/// Get方法请求
/// </summary>
/// <param name="url">请求地址</param>
/// <returns></returns>
public async Task<HttpResponseMessage> GetAsync(string url, List<KeyValuePair<string, string>> headers = null)
{
HttpRequestMessage request = new HttpRequestMessage()
{
RequestUri = new Uri(url),
Method = HttpMethod.Get,
};
if (headers != null && headers.Count > 0)
{
request.Headers.Clear();
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}
return await httpClient.SendAsync(request);
}
/// <summary>
/// Post方法请求 application/x-www-form-urlencoded格式
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="paramList">参数集合</param>
/// <returns></returns>
public HttpResponseMessage Post(string url, List<KeyValuePair<String, String>> paramList, List<KeyValuePair<string, string>> headers = null)
{
FormUrlEncodedContent formUrlEncodedContent = new FormUrlEncodedContent(paramList);
if (headers != null && headers.Count > 0)
{
formUrlEncodedContent.Headers.Clear();
foreach (var header in headers)
{
formUrlEncodedContent.Headers.Add(header.Key, header.Value);
}
}
HttpResponseMessage response = httpClient.PostAsync(new Uri(url), formUrlEncodedContent).Result;
return response;
}
public async Task<HttpResponseMessage> PostAsync(string url, List<KeyValuePair<String, String>> paramList, List<KeyValuePair<string, string>> headers = null)
{
FormUrlEncodedContent formUrlEncodedContent = new FormUrlEncodedContent(paramList);
if (headers != null && headers.Count > 0)
{
formUrlEncodedContent.Headers.Clear();
foreach (var header in headers)
{
formUrlEncodedContent.Headers.Add(header.Key, header.Value);
}
}
return await httpClient.PostAsync(new Uri(url), formUrlEncodedContent);
}
/// <summary>
/// Post方法请求 raw data
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="content">raw data</param>
/// <returns></returns>
public HttpResponseMessage Post(string url, string content, List<KeyValuePair<string, string>> headers = null)
{
StringContent stringContent = new StringContent(content, Encoding.UTF8);
if (headers != null && headers.Count > 0)
{
stringContent.Headers.Clear();
foreach (var header in headers)
{
//if (!header.Key.ToLower().Contains("accept") && header.Key!= "Cache-Control" && header.Key!= "Connection" && header.Key!= "Host" && header.Key != "User-Agent")
//{
// stringContent.Headers.TryAddWithoutValidation(header.Key, header.Value);
//}
stringContent.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
}
HttpResponseMessage response = httpClient.PostAsync(new Uri(url), stringContent).Result;
return response;
}
/// <summary>
/// Post方法请求 raw data
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="content">raw data</param>
/// <returns></returns>
public async Task<HttpResponseMessage> PostAsync(string url, string content, List<KeyValuePair<string, string>> headers = null)
{
StringContent stringContent = new StringContent(content, Encoding.UTF8);
if (headers != null && headers.Count > 0)
{
stringContent.Headers.Clear();
foreach (var header in headers)
{
stringContent.Headers.Add(header.Key, header.Value);
}
}
return await httpClient.PostAsync(new Uri(url), stringContent);
}
public HttpResponseMessage Post(string url, object data)
{
using (HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, url))
{
httpRequestMessage.Content = new ObjectContent<object>(data, new JsonMediaTypeFormatter());
HttpResponseMessage result = httpClient.SendAsync(httpRequestMessage).Result;
return result;
}
}
/// <summary>
/// 释放httpclient
/// </summary>
public void Release()
{
httpClient.Dispose();
}
/// <summary>
/// 设置默认请求头
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
public void SetDefaultHeaders(string name, string value)
{
httpClient.DefaultRequestHeaders.Add(name, value);
}
/// <summary>
/// 删除默认请求头
/// </summary>
/// <param name="name"></param>
public void RemoveDefaultHeaders(string name)
{
httpClient.DefaultRequestHeaders.Remove(name);
}
}
}