using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http.Headers;
namespace Utility.Job
{
///
/// 请求帮助类
///
public class HttpHelper
{
public static readonly HttpHelper Instance;
static HttpHelper()
{
Instance = new HttpHelper();
}
///
/// 不同url分配不同HttpClient
///
public static ConcurrentDictionary dictionary = new ConcurrentDictionary();
private HttpClient GetHttpClient(string url)
{
var uri = new Uri(url);
var key = uri.Scheme + uri.Host;
//if (!dictionary.Keys.Contains(key))
return dictionary.GetOrAdd(key, new HttpClient());
//return dictionary[key];
}
///
/// Post请求
///
/// url地址
/// 请求参数(Json字符串)
/// webapi做用户认证
///
public async Task PostAsync(string url, string jsonString, Dictionary headers = null)
{
if (string.IsNullOrWhiteSpace(jsonString))
jsonString = "{}";
StringContent content = new StringContent(jsonString);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
if (headers != null && headers.Count > 0)
{
//如果有headers认证等信息,则每个请求实例一个HttpClient
using (HttpClient http = new HttpClient())
{
foreach (var item in headers)
{
http.DefaultRequestHeaders.Remove(item.Key);
http.DefaultRequestHeaders.TryAddWithoutValidation(item.Key, item.Value);
}
return await http.PostAsync(new Uri(url), content);
}
}
else
{
return await GetHttpClient(url).PostAsync(new Uri(url), content);
}
}
///
/// Post请求
///
///
/// url地址
/// 请求参数
/// webapi做用户认证
///
public async Task PostAsync(string url, T content, Dictionary headers = null) where T : class
{
return await PostAsync(url, JsonConvert.SerializeObject(content), headers);
}
///
/// Get请求
///
/// url地址
/// webapi做用户认证
///
public async Task GetAsync(string url, Dictionary headers = null)
{
if (headers != null && headers.Count > 0)
{
//如果有headers认证等信息,则每个请求实例一个HttpClient
using (HttpClient http = new HttpClient())
{
foreach (var item in headers)
{
http.DefaultRequestHeaders.Remove(item.Key);
http.DefaultRequestHeaders.TryAddWithoutValidation(item.Key, item.Value);
}
return await http.GetAsync(url);
}
}
else
{
return await GetHttpClient(url).GetAsync(url);
}
}
///
/// Put请求
///
/// url地址
/// 请求参数(Json字符串)
/// webapi做用户认证
///
public async Task PutAsync(string url, string jsonString, Dictionary headers = null)
{
if (string.IsNullOrWhiteSpace(jsonString))
jsonString = "{}";
StringContent content = new StringContent(jsonString);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
if (headers != null && headers.Count > 0)
{
//如果有headers认证等信息,则每个请求实例一个HttpClient
using (HttpClient http = new HttpClient())
{
foreach (var item in headers)
{
http.DefaultRequestHeaders.Remove(item.Key);
http.DefaultRequestHeaders.TryAddWithoutValidation(item.Key, item.Value);
}
return await http.PutAsync(url, content);
}
}
else
{
return await GetHttpClient(url).PutAsync(url, content);
}
}
///
/// Put请求
///
///
/// url地址
/// 请求参数
/// webapi做用户认证
///
public async Task PutAsync(string url, T content, Dictionary headers = null)
{
return await PutAsync(url, JsonConvert.SerializeObject(content), headers);
}
///
/// Delete请求
///
///
/// webapi做用户认证
///
public async Task DeleteAsync(string url, Dictionary headers = null)
{
if (headers != null && headers.Count > 0)
{
//如果有headers认证等信息,则每个请求实例一个HttpClient
using (HttpClient http = new HttpClient())
{
foreach (var item in headers)
{
http.DefaultRequestHeaders.Remove(item.Key);
http.DefaultRequestHeaders.TryAddWithoutValidation(item.Key, item.Value);
}
return await http.DeleteAsync(url);
}
}
else
{
return await GetHttpClient(url).DeleteAsync(url);
}
}
}
}