wanshenmean
3 天以前 5e851678cc02257bbbd179446de36082430ca5bc
Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_Core/Helper/HttpHelper.cs
@@ -6,6 +6,7 @@
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Core.Const;
using WIDESEAWCS_Core.LogHelper;
namespace WIDESEAWCS_Core.Helper
@@ -24,7 +25,7 @@
        /// 方法会自动记录请求日志,包含请求地址、响应内容和耗时等信息
        /// 默认请求超时时间为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;
@@ -39,7 +40,8 @@
                        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)
@@ -63,7 +65,7 @@
        /// <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;
@@ -86,7 +88,8 @@
                            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;
            }
@@ -111,7 +114,7 @@
        /// /// <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;
@@ -126,7 +129,8 @@
                        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)
@@ -151,7 +155,7 @@
        /// <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;
@@ -174,10 +178,12 @@
                            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
                    {