using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.Http.Headers; using WIDESEA_Core.LogHelper; using System.Security.Cryptography; namespace WIDESEA_Core.Helper { public class HttpMesHelper { public static string Post(string serviceAddress, string requestJson = "", string contentType = "application/json", Dictionary? 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, 30); string LoginToken = AppSettings.Get("MESLoginToken"); headers = new Dictionary { //正式 { "LoginToken", LoginToken } //测试 //{ "LoginToken", "Z6_BusModel_LLD:5_92e848cf-b0b8-4f7e-9ac4-41e015c1fca3" } }; if (headers != null) { foreach (var header in headers) httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); } HttpResponseMessage responseMessage = httpClient.PostAsync(serviceAddress, httpContent).Result; result = responseMessage.Content.ReadAsStringAsync().Result; } return result; } catch (Exception e) { throw new Exception(e.Message); } finally { Logger.Add(serviceAddress, requestJson == null ? "" : requestJson, result, beginDate); } } } }