using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http.Headers; using System.Security.Policy; using System.Text; using System.Threading.Tasks; using WIDESEAWCS_Core.Const; using WIDESEAWCS_Core.LogHelper; namespace WIDESEAWCS_Core.Helper { public class HttpHelper { /// /// 异步发送HTTP GET请求并返回响应内容 /// /// 请求的服务地址 /// 响应内容类型,默认为application/json /// 请求头字典,可选参数 /// 返回请求结果的字符串内容 /// 当请求过程中发生错误时抛出异常 /// /// 方法会自动记录请求日志,包含请求地址、响应内容和耗时等信息 /// 默认请求超时时间为60秒 /// public static async Task GetAsync(string serviceAddress, string contentType = "application/json", int timeOut = CommunicationConst.HttpDefaultTimeoutSeconds, Dictionary? headers = null) { string result = string.Empty; DateTime beginDate = DateTime.Now; try { using HttpClient httpClient = new HttpClient(); httpClient.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); } HttpResponseMessage response = await httpClient.GetAsync(serviceAddress); result = await response.Content.ReadAsStringAsync(); return result; } catch (Exception e) { throw new Exception(e.Message); } finally { Logger.Add(serviceAddress, "", result, beginDate); } } /// /// 异步发送HTTP POST请求 /// /// 服务地址 /// 请求JSON数据 /// 内容类型,默认为application/json /// 请求头字典 /// 返回响应内容字符串,失败时抛出异常 /// /// 自动记录请求日志,包含请求地址、请求数据、响应数据和耗时 /// public static async Task PostAsync(string serviceAddress, string requestJson = null, string contentType = "application/json", int timeOut = CommunicationConst.HttpDefaultTimeoutSeconds, Dictionary? headers = null) { string result = string.Empty; DateTime beginDate = DateTime.Now; try { if (string.IsNullOrEmpty(requestJson)) { requestJson = ""; } using (HttpContent httpContent = new StringContent(requestJson)) { httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); using HttpClient httpClient = new HttpClient(); httpClient.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); } HttpResponseMessage response = await httpClient.PostAsync(serviceAddress, httpContent); result = await response.Content.ReadAsStringAsync(); } return result; } catch (Exception e) { throw new Exception(e.Message); } finally { Logger.Add(serviceAddress, requestJson == null ? "" : requestJson, result, beginDate); } } /// /// 发送HTTP GET请求到指定服务地址 /// /// 请求的服务地址 /// 请求内容类型,默认为application/json /// 请求头字典,可选参数 /// 返回服务响应字符串 /// 当请求发生错误时抛出异常 /// /// /// 该方法会自动记录请求日志,包含请求地址、请求数据、响应数据和耗时 /// public static string Get(string serviceAddress, string contentType = "application/json", int timeOut = CommunicationConst.HttpDefaultTimeoutSeconds, Dictionary? headers = null) { string result = string.Empty; DateTime beginDate = DateTime.Now; try { using HttpClient httpClient = new HttpClient(); httpClient.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); } Task task = httpClient.GetStringAsync(serviceAddress); result = task.GetAwaiter().GetResult(); return result; } catch (Exception e) { throw new Exception(e.Message); } finally { Logger.Add(serviceAddress, "", result, beginDate); } } /// /// 发送HTTP POST请求到指定服务地址 /// /// 服务地址URL /// 请求的JSON数据 /// 内容类型,默认为application/json /// 请求头字典 /// 返回服务端响应内容 /// 当请求发生异常时抛出 /// /// 该方法会自动记录请求日志,包含请求地址、请求数据、响应数据和耗时 /// public static string Post(string serviceAddress, string requestJson = null, string contentType = "application/json", int timeOut = CommunicationConst.HttpDefaultTimeoutSeconds, Dictionary? headers = null) { string result = string.Empty; DateTime beginDate = DateTime.Now; try { if (string.IsNullOrEmpty(requestJson)) { requestJson = ""; } using (HttpContent httpContent = new StringContent(requestJson)) { httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); using HttpClient httpClient = new HttpClient(); httpClient.Timeout = new TimeSpan(0, 0, timeOut); if (headers != null) { foreach (var header in headers) httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); } Task postTask = httpClient.PostAsync(serviceAddress, httpContent); HttpResponseMessage message = postTask.GetAwaiter().GetResult(); if (message.StatusCode == HttpStatusCode.OK) { Task readTask = message.Content.ReadAsStringAsync(); result = readTask.GetAwaiter().GetResult(); } else { result = ("异常,响应码:" + ((int)message.StatusCode).ToString() + ",原因:" + message.ReasonPhrase); throw new Exception(result); } } return result; } catch (Exception e) { result = "异常," + e.Message; throw new Exception(e.Message); } finally { Logger.Add(serviceAddress, requestJson == null ? "" : requestJson, result, beginDate); } } } }