From 4f39dcc195f28fa275fc2d065fbf1bf6a46c21b7 Mon Sep 17 00:00:00 2001 From: dengjunjie <dengjunjie@hnkhzn.com> Date: 星期二, 17 六月 2025 00:41:18 +0800 Subject: [PATCH] 优化出入库逻辑 --- 代码管理/WCS/WIDESEAWCS_Server/WIDESEAWCS_Common/Http/HttpHelpers.cs | 42 +++++++++++++++++++++++++++++++++++++----- 1 files changed, 37 insertions(+), 5 deletions(-) diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WIDESEAWCS_Server/WIDESEAWCS_Common/Http/HttpHelpers.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WIDESEAWCS_Server/WIDESEAWCS_Common/Http/HttpHelpers.cs" index 5380000..f931036 100644 --- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WCS/WIDESEAWCS_Server/WIDESEAWCS_Common/Http/HttpHelpers.cs" +++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/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; } } -- Gitblit v1.9.3