| | |
| | | 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; |