using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace GxPress.Common.Http
{
///
/// HttpClient
///
public static class HttpClientHelper
{
///
/// get
///
///
///
///
public static async Task GetHeadersAsync(string url,Dictionary 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;
}
///
/// 根据地址下载
///
///
///
public static async Task GetByteArrayAsync(string url)
{
HttpClient httpClient = new HttpClient(); //http对象
var response = await httpClient.GetByteArrayAsync(url);
return response;
}
///
/// post
///
///
/// json
///
///
public static async Task PostAsync(string url, object jsonObj,
Dictionary 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;
}
///
/// rest删除
///
///
///
///
public static async Task DeleteAsync(string url, Dictionary 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;
}
}
}