647556386
2025-10-18 d01658c63cd541fe4ea5cec5c4bd7f23b9408cdb
WMS/WIDESEA_WMSServer/WIDESEA_Core/Helper/HttpHelper.cs
@@ -6,6 +6,7 @@
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.LogHelper;
namespace WIDESEA_Core.Helper
{
@@ -59,38 +60,53 @@
        public static string Get(string serviceAddress, string contentType = "application/json", Dictionary<string, string>? headers = null)
        {
            string text = string.Empty;
            DateTime now = DateTime.Now;
            try
            {
                string result = string.Empty;
                using HttpClient httpClient = new HttpClient();
                httpClient.Timeout = new TimeSpan(0, 0, 60);
                if (headers != null)
                {
                    foreach (KeyValuePair<string, string> header in headers)
                    {
                        httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
                    }
                }
                foreach (var header in headers)
                    httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
                result = httpClient.GetStringAsync(serviceAddress).Result;
                return result;
                text = httpClient.GetStringAsync(serviceAddress).Result;
                return text;
            }
            catch (Exception e)
            catch (Exception ex)
            {
                Console.WriteLine(e.Message);
                throw new Exception(ex.Message);
            }
            return null;
            finally
            {
                Logger.Add(serviceAddress, "", text, now);
            }
        }
        public static string Post(string serviceAddress, string requestJson = null, string contentType = "application/json", Dictionary<string, string>? headers = null)
        public static string Post(string serviceAddress, string requestJson = "", string contentType = "application/json", Dictionary<string, string>? headers = null)
        {
            string result = string.Empty;
            DateTime beginDate = DateTime.Now;
            try
            {
                string result = string.Empty;
                using (HttpContent httpContent = new StringContent(requestJson))
                {
                    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    using HttpClient httpClient = new HttpClient();
                    httpClient.Timeout = new TimeSpan(0, 0, 60);
                    foreach (var header in headers)
                        httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
                    if (serviceAddress.Contains("cimforce/AtomJsonService"))
                    {
                        httpClient.Timeout = new TimeSpan(0, 0, 30);
                    }
                    if (headers != null)
                    {
                        foreach (var header in headers)
                            httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
                    }
                    result = httpClient.PostAsync(serviceAddress, httpContent).Result.Content.ReadAsStringAsync().Result;
                }
@@ -98,9 +114,12 @@
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw new Exception(e.Message);
            }
            return null;
            finally
            {
                //Logger.Add(serviceAddress, requestJson, result, beginDate);
            }
        }
        /// <summary>
        /// post请求
@@ -111,6 +130,7 @@
        /// <returns></returns>
        public static T Post<T>(string url, object parm, string rquestName = "") where T : class
        {
            DateTime beginDate = DateTime.Now;
            HttpWebResponse response = null;
            StreamReader resultReader = null;
            string responseContent = string.Empty;
@@ -129,7 +149,7 @@
                };
                response = (HttpWebResponse)request.GetResponse();
                Stream webStream = response.GetResponseStream();
               Stream webStream = response.GetResponseStream();
                if (webStream == null)
                {
                    throw new Exception("Network error");
@@ -138,6 +158,7 @@
                int statsCode = (int)response.StatusCode;
                resultReader = new StreamReader(webStream, Encoding.UTF8);
                responseContent = resultReader.ReadToEnd();
                if (response != null)
                    response.Close();
@@ -148,13 +169,16 @@
                {
                    throw new Exception("异常,响应码:" + statsCode.ToString());
                }
                return JsonConvert.DeserializeObject<T>(responseContent);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Logger.Add(url, parm, responseContent, beginDate);
            }
        }
    }