| | |
| | | using Microsoft.Extensions.Logging; |
| | | using Microsoft.Extensions.Logging; |
| | | using Newtonsoft.Json; |
| | | using System; |
| | | using System.Collections.Generic; |
| | |
| | | Logger.Add(serviceAddress, requestJson, result, beginDate); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// POST请æ±ï¼å¼æ¥çæ¬ï¼- è§£å³405é®é¢ |
| | | /// </summary> |
| | | /// <param name="serviceAddress">æå¡å°å</param> |
| | | /// <param name="requestJson">请æ±JSON</param> |
| | | /// <param name="contentType">å
容类å</param> |
| | | /// <param name="headers">请æ±å¤´</param> |
| | | /// <returns>ååºç»æ</returns> |
| | | /// <summary> |
| | | /// POST请æ±ï¼åæ¥çæ¬ï¼å
¼å®¹åæä»£ç ï¼ |
| | | /// </summary> |
| | | // 妿ä¸é¢çæ¹æ³è¿æé®é¢ï¼ä½¿ç¨è¿ä¸ªç®åçæ¬ |
| | | public static string PostSync(string serviceAddress, string requestJson = "", string contentType = "application/json", Dictionary<string, string>? headers = null) |
| | | { |
| | | string result = string.Empty; |
| | | DateTime beginDate = DateTime.Now; |
| | | try |
| | | { |
| | | using (var httpContent = new StringContent(requestJson, Encoding.UTF8, contentType)) |
| | | using (var httpClient = new HttpClient()) |
| | | { |
| | | httpClient.Timeout = TimeSpan.FromSeconds(600); |
| | | |
| | | // 设置é»è®¤è¯·æ±å¤´ - 使ç¨ç®åç User-Agent |
| | | httpClient.DefaultRequestHeaders.Add("Accept", "*/*"); |
| | | httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate"); |
| | | httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive"); |
| | | httpClient.DefaultRequestHeaders.Add("User-Agent", "WMS-Client/1.0"); // ç®åç User-Agent |
| | | |
| | | if (headers != null) |
| | | { |
| | | foreach (var header in headers) |
| | | httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
| | | } |
| | | |
| | | var response = httpClient.PostAsync(serviceAddress, httpContent).Result; |
| | | |
| | | Console.WriteLine($"HTTPç¶æç : {(int)response.StatusCode} - {response.StatusCode}"); |
| | | |
| | | if (!response.IsSuccessStatusCode) |
| | | { |
| | | throw new HttpRequestException($"HTTP请æ±å¤±è´¥: {(int)response.StatusCode} - {response.StatusCode}"); |
| | | } |
| | | |
| | | result = response.Content.ReadAsStringAsync().Result; |
| | | return result; |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | throw new Exception($"POST请æ±å¤±è´¥: {e.Message}"); |
| | | } |
| | | finally |
| | | { |
| | | Logger.Add(serviceAddress, requestJson, result, beginDate); |
| | | } |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// GETè¯·æ± |
| | | /// </summary> |
| | | /// <param name="serviceAddress">æå¡å°å</param> |
| | | /// <param name="parameters">请æ±åæ°</param> |
| | | /// <param name="headers">请æ±å¤´</param> |
| | | /// <returns>ååºç»æ</returns> |
| | | public static string GetRequest(string serviceAddress, Dictionary<string, string>? parameters = null, Dictionary<string, string>? headers = null) |
| | | { |
| | | string result = string.Empty; |
| | | DateTime beginDate = DateTime.Now; |
| | | try |
| | | { |
| | | using (var httpClient = new HttpClient()) |
| | | { |
| | | httpClient.Timeout = TimeSpan.FromSeconds(60); |
| | | |
| | | // 设置请æ±å¤´ |
| | | httpClient.DefaultRequestHeaders.Add("Accept", "*/*"); |
| | | httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate"); |
| | | httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive"); |
| | | httpClient.DefaultRequestHeaders.Add("User-Agent", "WMS-Client/1.0"); |
| | | |
| | | if (headers != null) |
| | | { |
| | | foreach (var header in headers) |
| | | httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
| | | } |
| | | |
| | | // æå»ºå¸¦åæ°ç URL |
| | | string url = serviceAddress; |
| | | if (parameters != null && parameters.Count > 0) |
| | | { |
| | | var queryParams = string.Join("&", parameters.Select(p => $"{Uri.EscapeDataString(p.Key)}={Uri.EscapeDataString(p.Value)}")); |
| | | url += (url.Contains("?") ? "&" : "?") + queryParams; |
| | | } |
| | | |
| | | var response = httpClient.GetAsync(url).Result; |
| | | |
| | | Console.WriteLine($"GET ç¶æç : {(int)response.StatusCode} - {response.StatusCode}"); |
| | | |
| | | if (!response.IsSuccessStatusCode) |
| | | { |
| | | throw new HttpRequestException($"GET 请æ±å¤±è´¥: {(int)response.StatusCode} - {response.StatusCode}"); |
| | | } |
| | | |
| | | result = response.Content.ReadAsStringAsync().Result; |
| | | return result; |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | throw new Exception($"GET 请æ±å¤±è´¥: {e.Message}"); |
| | | } |
| | | finally |
| | | { |
| | | Logger.Add(serviceAddress, parameters != null ? JsonConvert.SerializeObject(parameters) : "No Parameters", result, beginDate); |
| | | } |
| | | } |
| | | } |
| | | } |