1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- using System.Collections.Generic;
- using System.Net.Http;
- using System.Threading.Tasks;
- namespace GxPress.Common.Http
- {
- /// <summary>
- /// HttpClient
- /// </summary>
- public static class HttpClientHelper
- {
- /// <summary>
- /// get
- /// </summary>
- /// <param name="url"></param>
- /// <param name="headers"></param>
- /// <returns></returns>
- public static async Task<HttpResponseMessage> GetHeadersAsync(string url, Dictionary<string, string> headers)
- {
- var httpClientHandler = new HttpClientHandler { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator };
- HttpClient httpClient = new HttpClient(httpClientHandler); //http对象
- //添加header
- foreach (var header in headers)
- {
- httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
- }
- var response = await httpClient.GetAsync(url);
- return response;
- }
- /// <summary>
- /// 根据地址下载
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- public static async Task<byte[]> GetByteArrayAsync(string url)
- {
- var httpClientHandler = new HttpClientHandler { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator };
- HttpClient httpClient = new HttpClient(httpClientHandler); //http对象
- var response = await httpClient.GetByteArrayAsync(url);
- return response;
- }
- /// <summary>
- /// post
- /// </summary>
- /// <param name="url"></param>
- /// <param name="jsonObj">json</param>
- /// <param name="headers"></param>
- /// <returns></returns>
- public static async Task<HttpResponseMessage> PostAsync(string url, object jsonObj,
- Dictionary<string, string> headers = null)
- {
- var httpClientHandler = new HttpClientHandler { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator };
- HttpClient httpClient = new HttpClient(httpClientHandler); //http对象
- if (headers != null)
- foreach (var header in headers)
- {
- httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
- }
- HttpResponseMessage response = await httpClient.PostAsync(url,
- new JsonContent(jsonObj));
- return response;
- }
- /// <summary>
- /// rest删除
- /// </summary>
- /// <param name="url"></param>
- /// <param name="headers"></param>
- /// <returns></returns>
- public static async Task<HttpResponseMessage> DeleteAsync(string url, Dictionary<string, string> headers = null)
- {
- var httpClientHandler = new HttpClientHandler { ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator };
- HttpClient httpClient = new HttpClient(httpClientHandler); //http对象
- if (headers != null)
- foreach (var header in headers)
- {
- httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
- }
- HttpResponseMessage response = await httpClient.DeleteAsync(url);
- return response;
- }
- }
- }
|