From 850105e8cdf0d048ef843d87520513ac77a64200 Mon Sep 17 00:00:00 2001 From: huangxiaoqiang <huangxiaoqiang@hnkhzn.com> Date: 星期三, 23 七月 2025 21:21:53 +0800 Subject: [PATCH] 代码提交 --- 项目代码/WCS/WIDESEAWCS_Server/WIDESEAWCS_Common/Http/HttpHelper.cs | 136 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 136 insertions(+), 0 deletions(-) diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WIDESEAWCS_Server/WIDESEAWCS_Common/Http/HttpHelper.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WIDESEAWCS_Server/WIDESEAWCS_Common/Http/HttpHelper.cs" index 1568268..8079e70 100644 --- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WIDESEAWCS_Server/WIDESEAWCS_Common/Http/HttpHelper.cs" +++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WCS/WIDESEAWCS_Server/WIDESEAWCS_Common/Http/HttpHelper.cs" @@ -1,5 +1,6 @@ 锘縰sing Newtonsoft.Json; using System.Net; +using System.Net.Http.Headers; using System.Text; using WIDESEA_Common.Log; @@ -9,6 +10,141 @@ { private const int Timeout = 10 * 1000; + + public static async Task<string> GetAsync(string serviceAddress, Dictionary<string, object> parameters, string contentType = "application/json", Dictionary<string, string>? headers = null) + { + try + { + string result = string.Empty; + using HttpClient httpClient = new HttpClient(); + httpClient.Timeout = new TimeSpan(0, 0, 60); + // 灏嗗弬鏁版嫾鎺ュ埌URL涓� + string queryString = string.Join("&", parameters.Select(x => $"{x.Key}={x.Value}")); + serviceAddress += "?" + queryString; + + if (headers != null) + { + foreach (var header in headers) + httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); + } + + result = await httpClient.GetAsync(serviceAddress).Result.Content.ReadAsStringAsync(); + return result; + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + return null; + } + + + // 鐢ㄤ簬杩借釜姣忎釜璇锋眰鐨勮皟鐢ㄦ鏁板拰鏈�鍚庤姹傛椂闂淬�� + private static readonly Dictionary<string, (int Count, DateTime LastRequestTime)> requestTracker = new(); + + public static async Task<string> PostAsync(string serviceAddress, string requestJson = null, string contentType = "application/json", Dictionary<string, string>? headers = null) + { + try + { + if (serviceAddress.Contains("http://c24-cellmi3:12020")) + { + // 灏� JSON 瀛楃涓茶浆鎹负瀛楀吀 + var parameters = JsonConvert.DeserializeObject<Dictionary<string, object>>(requestJson); + + // 鍒涘缓涓�涓柊鐨勫瓧鍏革紝鎺掗櫎 RequestTime 鍜� SessionId + var filteredParameters = parameters.Where(p => p.Key != "RequestTime" && p.Key != "SessionId").ToDictionary(p => p.Key, p => p.Value); + + string requestKey = $"{serviceAddress}:{JsonConvert.SerializeObject(filteredParameters)}"; + + // 妫�鏌ヨ姹傛鏁板拰鏃堕棿闄愬埗 + if (requestTracker.TryGetValue(requestKey, out var requestInfo)) + { + if (requestInfo.Count >= 5 && DateTime.Now < requestInfo.LastRequestTime.AddMinutes(3)) + { + // 濡傛灉璇锋眰娆℃暟瓒呰繃闄愬埗涓旀湭瓒呰繃10鍒嗛挓锛屾姏鍑哄紓甯� + throw new InvalidOperationException("璇锋眰娆℃暟宸茶揪鍒伴檺鍒讹紝璇风◢鍚庡啀璇曘��"); + } + } + + // 鏇存柊璇锋眰璺熻釜淇℃伅 + if (requestTracker.ContainsKey(requestKey)) + { + requestTracker[requestKey] = (requestInfo.Count + 1, DateTime.Now); + } + else + { + requestTracker[requestKey] = (1, DateTime.Now); + } + } + 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, 120); + + if (headers != null) + { + foreach (var header in headers) + httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); + } + result = await httpClient.PostAsync(serviceAddress, httpContent).Result.Content.ReadAsStringAsync(); + } + return result; + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + return null; + } + + public static string Get(string serviceAddress, string contentType = "application/json", Dictionary<string, string>? headers = null) + { + 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) + { + Console.WriteLine(e.Message); + } + return null; + } + + public static string Post(string serviceAddress, string requestJson = null, string contentType = "application/json", Dictionary<string, string>? headers = null) + { + 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); + + result = httpClient.PostAsync(serviceAddress, httpContent).Result.Content.ReadAsStringAsync().Result; + } + return result; + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + return null; + } + /// <summary> /// post璇锋眰 /// </summary> -- Gitblit v1.9.3