12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- 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)
- {
- HttpClient httpClient = new HttpClient(); //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)
- {
- HttpClient httpClient = new HttpClient(); //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)
- {
- HttpClient httpClient = new HttpClient(); //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)
- {
- HttpClient httpClient = new HttpClient(); //http对象
- if (headers != null)
- foreach (var header in headers)
- {
- httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
- }
- HttpResponseMessage response = await httpClient.DeleteAsync(url);
- return response;
- }
- }
- }
|