| | |
| | | using Newtonsoft.Json; |
| | | using System.Net; |
| | | using System.Net.Http.Headers; |
| | | using System.Text; |
| | | using WIDESEA_Common.Log; |
| | | |
| | |
| | | { |
| | | 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> |