| | |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Net.Http.Headers; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | |
| | |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | public static async Task<string> PostAsync(string url, string requestJson = null, string contentType = "application/json", Dictionary<string, string>? headers = null) |
| | | { |
| | | try |
| | | { |
| | | LogRequestParameters(requestJson, url); |
| | | |
| | | 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); |
| | | |
| | | if (headers != null) |
| | | { |
| | | foreach (var header in headers) |
| | | httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); |
| | | } |
| | | result = await httpClient.PostAsync(url, httpContent).Result.Content.ReadAsStringAsync(); |
| | | } |
| | | LogResponseParameters(result, url); |
| | | |
| | | return result; |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | LogErrorParameters(ex.StackTrace, ex.Message, url); |
| | | Console.WriteLine(ex.Message); |
| | | } |
| | | return null; |
| | | } |
| | | private static void LogErrorParameters(string errordetail, string error, string url = "") |
| | | { |
| | | StringBuilder builder = new StringBuilder(); |
| | |
| | | LogFactory.GetLog("API鎺ュ彛").Info(true, builder); |
| | | } |
| | | |
| | | private static void LogRequestParameters(string parameters, string url = "") |
| | | { |
| | | StringBuilder builder = new StringBuilder(); |
| | | builder.Append(Environment.NewLine); |
| | | builder.Append("---------------------------------------------"); |
| | | builder.Append(Environment.NewLine); |
| | | builder.Append("url:" + url + "璇锋眰鍙傛暟: " + JsonConvert.SerializeObject(parameters)); |
| | | LogFactory.GetLog("API鎺ュ彛").Info(true, builder); |
| | | } |
| | | |
| | | private static void LogResponseParameters(string responseBody, string url = "") |
| | | { |
| | | StringBuilder builder = new StringBuilder(); |