| | |
| | | using System; |
| | | using AngleSharp.Dom; |
| | | using Newtonsoft.Json; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Net.Http.Headers; |
| | | using System.Security.Policy; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | |
| | |
| | | { |
| | | public class HttpHelper |
| | | { |
| | | public static async Task<string> GetAsync(string serviceAddress, string contentType = "application/json", Dictionary<string, string>? headers = null) |
| | | 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; |
| | | |
| | | foreach (var header in headers) |
| | | httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
| | | 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; |
| | |
| | | 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 |
| | | { |
| | | // 将 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(10)) |
| | | { |
| | | // 如果请求次数超过限制且未超过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)) |
| | | { |
| | |
| | | using HttpClient httpClient = new HttpClient(); |
| | | httpClient.Timeout = new TimeSpan(0, 0, 60); |
| | | |
| | | foreach (var header in headers) |
| | | httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
| | | |
| | | 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; |