SACenter/SA.Core/Util/HttpHelper.cs

169 lines
6.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SA.Core.Util
{
public class HttpHelper
{
public static bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
return true;
}
private static readonly HttpClient HttpClient;
static HttpHelper()
{
HttpClient = new HttpClient();
HttpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36");
}
/// <summary>
/// 发起POST同步请求
///
/// </summary>
/// <param name="url"></param>
/// <param name="postData"></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 , string contentType , int timeOut = 30, Dictionary<string, string>? headers =null)
{
postData = postData ?? "";
using (HttpContent httpContent = new StringContent(postData))
{
return HttpPost(url, httpContent, contentType, timeOut, headers);
}
}
public static string HttpPost(string url, HttpContent postData , string contentType , int timeOut = 30, Dictionary<string, string>? headers= null)
{
if (headers != null)
{
foreach (var header in headers)
HttpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
}
else
{
HttpClient.DefaultRequestHeaders.Clear();
}
if (!string.IsNullOrEmpty(contentType))
postData.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
// HttpClient.Timeout = new TimeSpan(0, 0, timeOut);
HttpResponseMessage response = HttpClient.PostAsync(url, postData).Result;
return response.Content.ReadAsStringAsync().Result;
}
public static HttpResponseMessage HttpDelete(string url, int timeOut = 30)
{
HttpResponseMessage response = HttpClient.DeleteAsync(url).Result;
return response;
}
public static string HttpPut(string url, string putData , int timeOut = 30)
{
using (HttpContent httpContent = new StringContent(putData, Encoding.UTF8))
{
HttpResponseMessage response = HttpClient.PutAsync(url, httpContent).Result;
return response.Content.ReadAsStringAsync().Result;
}
}
/// <summary>
/// 发起POST异步请求
/// </summary>
/// <param name="url"></param>
/// <param name="postData"></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 , string contentType, int timeOut = 30, Dictionary<string, string>? headers = null)
{
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();
}
}
}
public static string Send(HttpRequestMessage httpRequestMessage)
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = client.SendAsync(httpRequestMessage).Result;
return response.Content.ReadAsStringAsync().Result;
}
}
/// <summary>
/// 发起GET同步请求
/// </summary>
/// <param name="url"></param>
/// <param name="headers"></param>
/// <param name="contentType"></param>
/// <returns></returns>
public static string HttpGet(string url, Dictionary<string, string>? headers = null)
{
using (HttpClient client = new HttpClient())
{
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"></param>
/// <returns></returns>
public static async Task<string> HttpGetAsync(string url, Dictionary<string, string>? headers = null)
{
using (HttpClient client = new HttpClient())
{
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();
}
}
}
}