From 6f3ed7ad5ff997a5515d831d1cfb377e3dcbb72f Mon Sep 17 00:00:00 2001
From: huangxiaoqiang <huangxiaoqiang@hnkhzn.com>
Date: 星期二, 10 六月 2025 12:50:05 +0800
Subject: [PATCH] 1
---
CodeManagement/WMS/WIDESEA_WMSServer/WIDESEA_Common/HttpClient/HttpsClient.cs | 36 ++++++++++++++++++++++++++++++++----
1 files changed, 32 insertions(+), 4 deletions(-)
diff --git a/CodeManagement/WMS/WIDESEA_WMSServer/WIDESEA_Common/HttpClient/HttpsClient.cs b/CodeManagement/WMS/WIDESEA_WMSServer/WIDESEA_Common/HttpClient/HttpsClient.cs
index c30c9aa..f1afa5f 100644
--- a/CodeManagement/WMS/WIDESEA_WMSServer/WIDESEA_Common/HttpClient/HttpsClient.cs
+++ b/CodeManagement/WMS/WIDESEA_WMSServer/WIDESEA_Common/HttpClient/HttpsClient.cs
@@ -5,11 +5,24 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using System.Timers;
+using Timer = System.Timers.Timer;
namespace WIDESEA_Common;
public class HttpsClient
{
+ private static readonly Timer cleanupTimer;
+
+ static HttpsClient()
+ {
+ // 璁剧疆瀹氭椂鍣紝姣�5鍒嗛挓娓呯悊涓�娆equestTracker鐨勬暟鎹�
+ cleanupTimer = new Timer(2 * 60 * 1000);
+ cleanupTimer.Elapsed += CleanupRequestTracker;
+ cleanupTimer.AutoReset = true;
+ cleanupTimer.Enabled = true;
+ }
+
private static readonly LogFactory LogFactory = new LogFactory();
// 灏佽涓�涓敤HttpClient鍙戦�丟ET璇锋眰鐨勬柟娉曟湁鍙傛暟
@@ -44,23 +57,24 @@
// 鐢ㄤ簬杩借釜姣忎釜璇锋眰鐨勮皟鐢ㄦ鏁板拰鏈�鍚庤姹傛椂闂淬��
private static readonly Dictionary<string, (int Count, DateTime LastRequestTime)> requestTracker = new();
+
// 灏佽涓�涓敤HttpClient鍙戦�丳ost璇锋眰鐨勬柟娉曟湁鍙傛暟
public static async Task<string> PostAsync(string url, Dictionary<string, object> parameters)
{
// 鍒涘缓涓�涓柊鐨勫瓧鍏革紝鎺掗櫎 RequestTime 鍜� SessionId
- var filteredParameters = parameters.Where(p => p.Key != "RequestTime"&& p.Key != "SessionId").ToDictionary(p => p.Key, p => p.Value);
+ var filteredParameters = parameters.Where(p => p.Key != "RequestTime" && p.Key != "SessionId").ToDictionary(p => p.Key, p => p.Value);
string requestKey = $"{url}:{JsonConvert.SerializeObject(filteredParameters)}";
- // 妫�鏌ヨ姹傛鏁板拰鏃堕棿闄愬埗
+ // 妫�鏌ヨ姹傛鏁板拰鏃堕棿闄愬埗
if (requestTracker.TryGetValue(requestKey, out var requestInfo))
{
- if (requestInfo.Count >= 5 && DateTime.Now < requestInfo.LastRequestTime.AddMinutes(3))
+ if (requestInfo.Count >= 10 && DateTime.Now < requestInfo.LastRequestTime.AddMinutes(2))
{
// 濡傛灉璇锋眰娆℃暟瓒呰繃闄愬埗涓旀湭瓒呰繃10鍒嗛挓锛屾姏鍑哄紓甯�
Console.WriteLine("璇锋眰娆℃暟宸茶揪鍒伴檺鍒讹紝璇风◢鍚庡啀璇曘��");
Console.WriteLine($"鎺ュ彛鍦板潃锛歿url}");
Console.WriteLine($"璇锋眰鏁版嵁锛歿JsonConvert.SerializeObject(filteredParameters, Formatting.Indented)}");
- throw new Exception($"鎺ュ彛鍦板潃锛歿url}---璇锋眰娆℃暟宸茶揪鍒伴檺鍒讹紝璇风◢鍚庡啀璇曘��");
+ throw new Exception($"鎺ュ彛鍦板潃锛歿url}---璇锋眰娆℃暟宸茶揪鍒伴檺鍒讹紝璇风◢鍚庡啀璇曘��");
}
}
@@ -137,4 +151,18 @@
}
}
+ // 娓呯悊requestTracker鐨勬柟娉�
+ private static void CleanupRequestTracker(object sender, ElapsedEventArgs e)
+ {
+ // 鑾峰彇褰撳墠鏃堕棿
+ DateTime now = DateTime.Now;
+
+ // 閬嶅巻骞舵竻鐞唕equestTracker涓秴杩�10鍒嗛挓鐨勮褰�
+ var keysToRemove = requestTracker.Keys.Where(key => now > requestTracker[key].LastRequestTime.AddMinutes(2)).ToList();
+
+ foreach (var key in keysToRemove)
+ {
+ requestTracker.Remove(key);
+ }
+ }
}
\ No newline at end of file
--
Gitblit v1.9.3