在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 下面是整个封装的HttpClient帮助类: using System; using System.Collections.Generic; using System.Net.Http; 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) { stringContent.Headers.Add(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); } /// <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); } } }
|
请发表评论