dengjunjie
6 天以前 4f39dcc195f28fa275fc2d065fbf1bf6a46c21b7
´úÂë¹ÜÀí/WCS/WIDESEAWCS_Server/WIDESEAWCS_Common/Http/HttpHelpers.cs
@@ -1,6 +1,7 @@

using Newtonsoft.Json;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using WIDESEA_Common.Log;
@@ -8,7 +9,7 @@
{
    public class HttpHelpers
    {
        private const int Timeout = 10 * 1000;
        private const int Timeout = 20 * 1000;
        /// <summary>
        /// post请求
@@ -62,11 +63,42 @@
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("System/API请求", rquestName, "请求异常", new { è¯·æ±‚报文 = parm, æŽ¥æ”¶æŠ¥æ–‡ = responseContent, é”™è¯¯ = ex.Message });
                WriteLog.Write_Log("API", rquestName, "请求异常", new { è¯·æ±‚报文 = parm, æŽ¥æ”¶æŠ¥æ–‡ = responseContent, é”™è¯¯ = ex.Message });
                throw ex;
            }
        }
        public static async Task<string?> PostAsync(string serviceAddress, string requestJson = null, string contentType = "application/json", Dictionary<string, string>? headers = null)
        {
            string result = string.Empty;
            DateTime beginDate = DateTime.Now;
            try
            {
                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);
                    if (headers != null)
                    {
                        foreach (KeyValuePair<string, string> header in headers)
                        {
                            httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
                        }
                    }
                    result = await httpClient.PostAsync(serviceAddress, httpContent).Result.Content.ReadAsStringAsync();
                }
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
            }
        }
        /// <summary>
        /// post请求
        /// </summary>
@@ -119,7 +151,7 @@
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("System/API请求", rquestName, "请求异常", new { è¯·æ±‚报文 = parm, æŽ¥æ”¶æŠ¥æ–‡ = responseContent, é”™è¯¯ = ex.Message });
                WriteLog.Write_Log("API", rquestName, "请求异常", new { è¯·æ±‚报文 = parm, æŽ¥æ”¶æŠ¥æ–‡ = responseContent, é”™è¯¯ = ex.Message });
                throw ex;
            }
        }
@@ -184,12 +216,12 @@
                    throw new Exception("异常,响应码:" + statsCode.ToString());
                }
                WriteLog.Write_Log("System/API请求", rquestName, "请求成功", new { è¯·æ±‚报文 = parm, æŽ¥æ”¶æŠ¥æ–‡ = responseContent });
                WriteLog.Write_Log("API", rquestName, "请求成功", new { è¯·æ±‚报文 = parm, æŽ¥æ”¶æŠ¥æ–‡ = responseContent });
                return JsonConvert.DeserializeObject<T>(responseContent);
            }
            catch (Exception ex)
            {
                WriteLog.Write_Log("System/API请求", rquestName, "请求异常", new { è¯·æ±‚报文 = parm, æŽ¥æ”¶æŠ¥æ–‡ = responseContent, é”™è¯¯ = ex.Message });
                WriteLog.Write_Log("API", rquestName, "请求异常", new { è¯·æ±‚报文 = parm, æŽ¥æ”¶æŠ¥æ–‡ = responseContent, é”™è¯¯ = ex.Message });
                throw ex;
            }
        }