using SqlSugar;
|
using System.Text;
|
using WIDESEAWCS_Code;
|
using WIDESEAWCS_Core.Helper;
|
|
namespace WIDESEAWCS_Core.Http
|
{
|
public static class HttpRequestHelper
|
{
|
/// <summary>
|
/// Òì²½·¢ËÍHTTPÇëÇó²¢·µ»ØÏìÓ¦ÄÚÈÝ
|
/// </summary>
|
/// <param name="serviceAddress">ÇëÇóµØÖ·</param>
|
/// <param name="method">ÇëÇó·½·¨</param>
|
/// <param name="requestContent">ÇëÇóÄÚÈÝ</param>
|
/// <param name="contentType">ÄÚÈÝÀàÐÍ</param>
|
/// <param name="timeOut">³¬Ê±Ê±¼ä£¨Ã룩</param>
|
/// <param name="headers">ÇëÇóÍ·</param>
|
/// <returns>ÏìÓ¦ÄÚÈÝ</returns>
|
public static async Task<string> SendAsync(string serviceAddress, HttpMethod method, string? requestContent = null, string contentType = "application/json", int timeOut = 60, Dictionary<string, string>? headers = null)
|
{
|
using HttpClient httpClient = new HttpClient();
|
httpClient.Timeout = new TimeSpan(0, 0, timeOut);
|
|
using HttpRequestMessage request = new HttpRequestMessage(method, serviceAddress);
|
if (!string.IsNullOrWhiteSpace(requestContent))
|
{
|
request.Content = new StringContent(requestContent, Encoding.UTF8, contentType);
|
}
|
|
if (headers != null)
|
{
|
foreach (var header in headers)
|
{
|
request.Headers.TryAddWithoutValidation(header.Key, header.Value);
|
}
|
}
|
|
using HttpResponseMessage response = await httpClient.SendAsync(request);
|
string result = await response.Content.ReadAsStringAsync();
|
|
if (!response.IsSuccessStatusCode)
|
{
|
throw new Exception($"Òì³££¬ÏìÓ¦Â룺{(int)response.StatusCode},ÔÒò£º{response.ReasonPhrase}");
|
}
|
|
return result;
|
}
|
|
/// <summary>
|
/// Òì²½·¢ËÍHTTP GETÇëÇó²¢·µ»ØÏìÓ¦ÄÚÈÝ
|
/// </summary>
|
public static Task<string> GetAsync(string serviceAddress, int timeOut = 60, Dictionary<string, string>? headers = null)
|
{
|
return SendAsync(serviceAddress, HttpMethod.Get, null, "application/json", timeOut, headers);
|
}
|
|
/// <summary>
|
/// Òì²½·¢ËÍHTTP POSTÇëÇó²¢·µ»ØÏìÓ¦ÄÚÈÝ
|
/// </summary>
|
public static Task<string> PostAsync(string serviceAddress, string? requestJson = null, string contentType = "application/json", int timeOut = 60, Dictionary<string, string>? headers = null)
|
{
|
return SendAsync(serviceAddress, HttpMethod.Post, requestJson, contentType, timeOut, headers);
|
}
|
|
/// <summary>
|
/// Òì²½·¢ËÍHTTP PUTÇëÇó²¢·µ»ØÏìÓ¦ÄÚÈÝ
|
/// </summary>
|
public static Task<string> PutAsync(string serviceAddress, string? requestJson = null, string contentType = "application/json", int timeOut = 60, Dictionary<string, string>? headers = null)
|
{
|
return SendAsync(serviceAddress, HttpMethod.Put, requestJson, contentType, timeOut, headers);
|
}
|
|
/// <summary>
|
/// Òì²½·¢ËÍHTTP DELETEÇëÇó²¢·µ»ØÏìÓ¦ÄÚÈÝ
|
/// </summary>
|
public static Task<string> DeleteAsync(string serviceAddress, int timeOut = 60, Dictionary<string, string>? headers = null)
|
{
|
return SendAsync(serviceAddress, HttpMethod.Delete, null, "application/json", timeOut, headers);
|
}
|
|
/// <summary>
|
/// ¸ù¾ÝÊý¾Ý¿âÅäÖ÷¢ËÍHTTPÇëÇó²¢·µ»ØÏìÓ¦ÄÚÈÝ
|
/// </summary>
|
/// <param name="requestContent">ÇëÇóÄÚÈÝ</param>
|
/// <param name="method">ÇëÇó·½·¨</param>
|
/// <param name="category">ÅäÖ÷ÖÀà</param>
|
/// <param name="categoryItem">ÅäÖÃÏî</param>
|
/// <param name="contentType">ÄÚÈÝÀàÐÍ</param>
|
/// <param name="timeOut">³¬Ê±Ê±¼ä£¨Ã룩</param>
|
/// <returns>ÏìÓ¦ÄÚÈÝ</returns>
|
public static async Task<string> SendAsync(string? requestContent, HttpMethod method, string? category, string? configKey, string contentType = "application/json", int timeOut = 60)
|
{
|
ISqlSugarClient? db = App.GetService<ISqlSugarClient>();
|
if (db == null)
|
{
|
throw new InvalidOperationException("Êý¾Ý¿â·þÎñδ³õʼ»¯");
|
}
|
|
var configs = await db.Ado.SqlQueryAsync<Sys_Config>(
|
"SELECT * FROM Sys_Config WHERE Category = @Category",
|
new { Category = category });
|
if (configs.IsNullOrEmpty())
|
{
|
throw new Exception($"δÕÒµ½ÅäÖ÷ÖÀࣺ{category}");
|
}
|
|
string address = string.Empty;
|
string servicePath = string.Empty;
|
Dictionary<string, string> configDict = new Dictionary<string, string>();
|
|
foreach (var config in configs)
|
{
|
if (config.CategoryItem == "Header")
|
{
|
configDict[config.ConfigKey] = config.ConfigValue;
|
|
continue;
|
}
|
|
if (config.ConfigKey == "BASE")
|
{
|
address = config.ConfigValue;
|
continue;
|
}
|
|
if (!string.IsNullOrWhiteSpace(configKey) && config.ConfigKey == configKey)
|
{
|
servicePath = config.ConfigValue;
|
}
|
}
|
|
if (string.IsNullOrWhiteSpace(address))
|
{
|
throw new Exception($"δÕÒµ½ÅäÖõØÖ·£º{category} - BASE");
|
}
|
|
string normalizedPath = string.IsNullOrWhiteSpace(servicePath)
|
? string.Empty
|
: (servicePath.StartsWith("/") ? servicePath : $"/{servicePath}");
|
|
string serviceAddress = $"{address}{normalizedPath}";
|
if (method == HttpMethod.Get)
|
{
|
return await GetAsync(serviceAddress, timeOut, configDict);
|
}
|
|
if (method == HttpMethod.Post)
|
{
|
return await PostAsync(serviceAddress, requestContent, contentType, timeOut, configDict);
|
}
|
|
if (method == HttpMethod.Put)
|
{
|
return await PutAsync(serviceAddress, requestContent, contentType, timeOut, configDict);
|
}
|
|
if (method == HttpMethod.Delete)
|
{
|
return await DeleteAsync(serviceAddress, timeOut, configDict);
|
}
|
|
throw new NotSupportedException($"²»Ö§³ÖµÄÇëÇó·½·¨£º{method}");
|
}
|
|
/// <summary>
|
/// °´ÅäÖ÷ÖÀà·¢ËÍGETÇëÇó
|
/// </summary>
|
public static Task<string> HTTPGetAsync(string category, string? requestContent, string? configKey, int timeOut = 60)
|
{
|
return SendAsync(requestContent, HttpMethod.Get, category, configKey, "application/json", timeOut);
|
}
|
|
/// <summary>
|
/// °´ÅäÖ÷ÖÀà·¢ËÍPOSTÇëÇó
|
/// </summary>
|
public static Task<string> HTTPPostAsync(string category, string? requestContent, string? configKey, int timeOut = 60)
|
{
|
return SendAsync(requestContent, HttpMethod.Post, category, configKey, "application/json", timeOut);
|
}
|
}
|
}
|