Http请求封装
using Newtonsoft.Json;
using System.Text;
namespace DockerTest.Controllers
{
/// <summary>
/// Http请求封装
/// </summary>
public class HttpHelper
{
#region Post请求
/// <summary>
/// 发起POST异步请求
/// </summary>
/// <param name="url"></param>
/// <param name="postData">请求json数据</param>
/// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
/// <param name="headers">请求头</param>
/// <returns>返回对象实体</returns>
public static async Task<T> HttpPostAsync<T>(string url, string? postData = null, Dictionary<string, string>? headers = null, int timeOut = 30, string contentType = "application/json")
{
var res = await HttpPostAsync(url, postData, headers, timeOut, contentType);
return res.ToEntity<T>();
}
/// <summary>
/// 发起POST同步请求
/// </summary>
/// <param name="url"></param>
/// <param name="postData">请求json数据</param>
/// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
/// <param name="headers">请求头</param>
/// <returns>返回对象实体</returns>
public static T HttpPost<T>(string url, string? postData = null, Dictionary<string, string>? headers = null, int timeOut = 30, string contentType = "application/json")
{
return HttpPost(url, postData, headers, timeOut, contentType).ToEntity<T>();
}
/// <summary>
/// 发起POST异步请求
/// </summary>
/// <param name="url"></param>
/// <param name="postData">请求json数据</param>
/// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
/// <param name="headers">请求头</param>
/// <returns>返回响应的字符串类型数据</returns>
public static async Task<string> HttpPostAsync(string url, string? postData = null, Dictionary<string, string>? headers = null, int timeOut = 30, string contentType = "application/json")
{
postData = postData ?? "";
using (HttpClient client = new HttpClient())
{
client.Timeout = new TimeSpan(0, 0, timeOut);
if (headers != null)
{
foreach (var header in headers)
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
{
if (contentType != null)
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
HttpResponseMessage response = await client.PostAsync(url, httpContent);
return await response.Content.ReadAsStringAsync();
}
}
}
/// <summary>
/// 发起POST同步请求
/// </summary>
/// <param name="url"></param>
/// <param name="postData">请求json数据</param>
/// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
/// <param name="headers">请求头</param>
/// <returns>返回响应的字符串类型数据</returns>
public static string HttpPost(string url, string? postData = null, Dictionary<string, string>? headers = null, int timeOut = 30, string contentType = "application/json")
{
postData = postData ?? "";
using (HttpClient client = new HttpClient())
{
if (headers != null)
{
foreach (var header in headers)
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
{
if (contentType != null)
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
return response.Content.ReadAsStringAsync().Result;
}
}
}
#endregion
#region Get请求
/// <summary>
/// 发起GET同步请求
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="headers">请求头</param>
/// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
/// <returns>返回对象实体</returns>
public static T HttpGet<T>(string url, Dictionary<string, string>? headers = null, string contentType = "application/json")
{
return HttpGet(url, headers, contentType).ToEntity<T>();
}
/// <summary>
/// 发起GET异步请求
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="headers">请求头</param>
/// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
/// <returns></returns>
public static async Task<T> HttpGetAsync<T>(string url, Dictionary<string, string>? headers = null, string contentType = "application/json")
{
var res = await HttpGetAsync(url, headers, contentType);
return res.ToEntity<T>();
}
/// <summary>
/// 发起GET同步请求
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="headers">请求头</param>
/// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
/// <returns>返回响应的字符串类型数据</returns>
public static string HttpGet(string url, Dictionary<string, string>? headers = null, string contentType = "application/json")
{
using (HttpClient client = new HttpClient())
{
if (contentType != null)
client.DefaultRequestHeaders.Add("ContentType", contentType);
if (headers != null)
{
foreach (var header in headers)
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
HttpResponseMessage response = client.GetAsync(url).Result;
return response.Content.ReadAsStringAsync().Result;
}
}
/// <summary>
/// 发起GET异步请求
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="headers">请求头</param>
/// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
/// <returns>返回响应的字符串类型数据</returns>
public static async Task<string> HttpGetAsync(string url, Dictionary<string, string>? headers = null, string contentType = "application/json")
{
using (HttpClient client = new HttpClient())
{
if (contentType != null)
client.DefaultRequestHeaders.Add("ContentType", contentType);
if (headers != null)
{
foreach (var header in headers)
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
HttpResponseMessage response = await client.GetAsync(url);
return await response.Content.ReadAsStringAsync();
}
}
#endregion
}
#region Json扩展类
/// <summary>
/// Json扩展方法
/// </summary>
public static class JsonExtends
{
public static T? ToEntity<T>(this string val)
{
return JsonConvert.DeserializeObject<T>(val);
}
public static string ToJson<T>(this T entity, Formatting formatting = Formatting.None)
{
return JsonConvert.SerializeObject(entity, formatting);
}
}
#endregion
}