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,23 +60,31 @@
        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);
                foreach (var header in headers)
                    httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
                result = httpClient.GetStringAsync(serviceAddress).Result;
                return result;
            }
            catch (Exception e)
                if (headers != null)
            {
                Console.WriteLine(e.Message);
                    foreach (KeyValuePair<string, string> header in headers)
                    {
                        httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
            }
            return null;
                }
                text = httpClient.GetStringAsync(serviceAddress).Result;
                return text;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                Logger.Add(serviceAddress, "", text, now);
            }
        }
        public static string Post(string serviceAddress, string requestJson = "", string contentType = "application/json", Dictionary<string, string>? headers = null)
@@ -121,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;
@@ -149,6 +159,7 @@
                resultReader = new StreamReader(webStream, Encoding.UTF8);
                responseContent = resultReader.ReadToEnd();
                if (response != null)
                    response.Close();
                if (resultReader != null)
@@ -158,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);
            }
        }
    }