/// <summary> /// 调用外部接口 /// </summary> /// <param name="request">消息体</param> /// <returns></returns> public static string CallExternalInterface(string request) { //若传输的消息体是集合或者单个实体都可以转为json字符串来进行传输 string result = null; string requestUrl = "http://127.0.0.1:xxxxx";//模拟请求的url //HttpClientHandler跳过SSL验证 HttpClientHandler httpClientHanlder = new HttpClientHandler() { ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true }; using (var httpClient = new HttpClient(httpClientHanlder)) { httpClient.Timeout = new TimeSpan(1, 0, 0, 0, 0); httpClient.DefaultRequestHeaders.Add("ContentType", "application/json"); httpClient.DefaultRequestHeaders.Add("Accept", "*/*"); HttpContent httpContent = new StringContent(request); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); //post请求 var response = httpClient.PostAsync(requestUrl, httpContent).GetAwaiter().GetResult(); if (response.IsSuccessStatusCode) { //解析响应体 响应消息 result = response.Content.ReadAsStringAsync().Result; } else { result = response.ToString(); } } return result; }