| | |
| | |  |
| | | using Newtonsoft.Json; |
| | | using System.Net; |
| | | using System.Net.Http.Headers; |
| | | using System.Text; |
| | | using WIDESEA_Common.Log; |
| | | |
| | |
| | | { |
| | | public class HttpHelpers |
| | | { |
| | | private const int Timeout = 10 * 1000; |
| | | private const int Timeout = 20 * 1000; |
| | | |
| | | /// <summary> |
| | | /// postè¯·æ± |
| | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | WriteLog.Write_Log("System/API请æ±", rquestName, "请æ±å¼å¸¸", new { è¯·æ±æ¥æ = parm, æ¥æ¶æ¥æ = responseContent, é误 = ex.Message }); |
| | | WriteLog.Write_Log("API", rquestName, "请æ±å¼å¸¸", new { è¯·æ±æ¥æ = parm, æ¥æ¶æ¥æ = responseContent, é误 = ex.Message }); |
| | | throw ex; |
| | | } |
| | | } |
| | | public static async Task<string?> PostAsync(string serviceAddress, string requestJson = null, string contentType = "application/json", Dictionary<string, string>? headers = null) |
| | | { |
| | | string result = string.Empty; |
| | | DateTime beginDate = DateTime.Now; |
| | | try |
| | | { |
| | | 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); |
| | | if (headers != null) |
| | | { |
| | | foreach (KeyValuePair<string, string> header in headers) |
| | | { |
| | | httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
| | | } |
| | | } |
| | | |
| | | result = await httpClient.PostAsync(serviceAddress, httpContent).Result.Content.ReadAsStringAsync(); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | throw new Exception(ex.Message); |
| | | } |
| | | finally |
| | | { |
| | | } |
| | | } |
| | | /// <summary> |
| | | /// postè¯·æ± |
| | | /// </summary> |
| | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | WriteLog.Write_Log("System/API请æ±", rquestName, "请æ±å¼å¸¸", new { è¯·æ±æ¥æ = parm, æ¥æ¶æ¥æ = responseContent, é误 = ex.Message }); |
| | | WriteLog.Write_Log("API", rquestName, "请æ±å¼å¸¸", new { è¯·æ±æ¥æ = parm, æ¥æ¶æ¥æ = responseContent, é误 = ex.Message }); |
| | | throw ex; |
| | | } |
| | | } |
| | |
| | | throw new Exception("å¼å¸¸ï¼ååºç ï¼" + statsCode.ToString()); |
| | | } |
| | | |
| | | WriteLog.Write_Log("System/API请æ±", rquestName, "è¯·æ±æå", new { è¯·æ±æ¥æ = parm, æ¥æ¶æ¥æ = responseContent }); |
| | | WriteLog.Write_Log("API", rquestName, "è¯·æ±æå", new { è¯·æ±æ¥æ = parm, æ¥æ¶æ¥æ = responseContent }); |
| | | return JsonConvert.DeserializeObject<T>(responseContent); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | WriteLog.Write_Log("System/API请æ±", rquestName, "请æ±å¼å¸¸", new { è¯·æ±æ¥æ = parm, æ¥æ¶æ¥æ = responseContent, é误 = ex.Message }); |
| | | WriteLog.Write_Log("API", rquestName, "请æ±å¼å¸¸", new { è¯·æ±æ¥æ = parm, æ¥æ¶æ¥æ = responseContent, é误 = ex.Message }); |
| | | throw ex; |
| | | } |
| | | } |