| | |
| | | using System.Security.Policy; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEAWCS_Core.Const; |
| | | using WIDESEAWCS_Core.LogHelper; |
| | | |
| | | namespace WIDESEAWCS_Core.Helper |
| | |
| | | /// 方法会自动记录请求日志,包含请求地址、响应内容和耗时等信息 |
| | | /// 默认请求超时时间为60秒 |
| | | /// </remarks> |
| | | public static async Task<string> GetAsync(string serviceAddress, string contentType = "application/json", int timeOut = 60, Dictionary<string, string>? headers = null) |
| | | public static async Task<string> GetAsync(string serviceAddress, string contentType = "application/json", int timeOut = CommunicationConst.HttpDefaultTimeoutSeconds, Dictionary<string, string>? headers = null) |
| | | { |
| | | string result = string.Empty; |
| | | DateTime beginDate = DateTime.Now; |
| | |
| | | httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
| | | } |
| | | |
| | | result = await httpClient.GetAsync(serviceAddress).Result.Content.ReadAsStringAsync(); |
| | | HttpResponseMessage response = await httpClient.GetAsync(serviceAddress); |
| | | result = await response.Content.ReadAsStringAsync(); |
| | | return result; |
| | | } |
| | | catch (Exception e) |
| | |
| | | /// <remarks> |
| | | /// 自动记录请求日志,包含请求地址、请求数据、响应数据和耗时 |
| | | /// </remarks> |
| | | public static async Task<string?> PostAsync(string serviceAddress, string requestJson = null, string contentType = "application/json", int timeOut = 60, Dictionary<string, string>? headers = null) |
| | | public static async Task<string?> PostAsync(string serviceAddress, string requestJson = null, string contentType = "application/json", int timeOut = CommunicationConst.HttpDefaultTimeoutSeconds, Dictionary<string, string>? headers = null) |
| | | { |
| | | string result = string.Empty; |
| | | DateTime beginDate = DateTime.Now; |
| | |
| | | httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
| | | } |
| | | |
| | | result = await httpClient.PostAsync(serviceAddress, httpContent).Result.Content.ReadAsStringAsync(); |
| | | HttpResponseMessage response = await httpClient.PostAsync(serviceAddress, httpContent); |
| | | result = await response.Content.ReadAsStringAsync(); |
| | | } |
| | | return result; |
| | | } |
| | |
| | | /// /// <remarks> |
| | | /// 该方法会自动记录请求日志,包含请求地址、请求数据、响应数据和耗时 |
| | | /// </remarks> |
| | | public static string Get(string serviceAddress, string contentType = "application/json", int timeOut = 60, Dictionary<string, string>? headers = null) |
| | | public static string Get(string serviceAddress, string contentType = "application/json", int timeOut = CommunicationConst.HttpDefaultTimeoutSeconds, Dictionary<string, string>? headers = null) |
| | | { |
| | | string result = string.Empty; |
| | | DateTime beginDate = DateTime.Now; |
| | |
| | | httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
| | | } |
| | | |
| | | result = httpClient.GetStringAsync(serviceAddress).Result; |
| | | Task<string> task = httpClient.GetStringAsync(serviceAddress); |
| | | result = task.GetAwaiter().GetResult(); |
| | | return result; |
| | | } |
| | | catch (Exception e) |
| | |
| | | /// <remarks> |
| | | /// 该方法会自动记录请求日志,包含请求地址、请求数据、响应数据和耗时 |
| | | /// </remarks> |
| | | public static string Post(string serviceAddress, string requestJson = null, string contentType = "application/json", int timeOut = 60, Dictionary<string, string>? headers = null) |
| | | public static string Post(string serviceAddress, string requestJson = null, string contentType = "application/json", int timeOut = CommunicationConst.HttpDefaultTimeoutSeconds, Dictionary<string, string>? headers = null) |
| | | { |
| | | string result = string.Empty; |
| | | DateTime beginDate = DateTime.Now; |
| | |
| | | httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
| | | } |
| | | |
| | | HttpResponseMessage message = httpClient.PostAsync(serviceAddress, httpContent).Result; |
| | | Task<HttpResponseMessage> postTask = httpClient.PostAsync(serviceAddress, httpContent); |
| | | HttpResponseMessage message = postTask.GetAwaiter().GetResult(); |
| | | if (message.StatusCode == HttpStatusCode.OK) |
| | | { |
| | | result = message.Content.ReadAsStringAsync().Result; |
| | | Task<string> readTask = message.Content.ReadAsStringAsync(); |
| | | result = readTask.GetAwaiter().GetResult(); |
| | | } |
| | | else |
| | | { |