您的位置:首页 > 新闻 > 会展 > httpclient访问https请求报错处理

httpclient访问https请求报错处理

2024/10/5 18:12:25 来源:https://blog.csdn.net/rainmanqqst/article/details/140206111  浏览:    关键词:httpclient访问https请求报错处理

C#通过httpclient调用https请求时,报错

错误信息为:The remote certificate is invalid according to the validation procedure

该错误是由于使用httpclient访问不合法的https站点导致出现的异常。

处理代码如下


public static string HttpPostWithToken(string url, string jsonStr, string token, int timeout = 15)
{string info = "";try{var handler = new HttpClientHandler() { };handler.ServerCertificateCustomValidationCallback += (sender, cert, chain, sslPolicyErrors) => { return true; };handler.SslProtocols = SslProtocols.None;HttpClient httpClient = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(timeout) };var message = new HttpRequestMessage(HttpMethod.Post, url);httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("AU",token);if (!string.IsNullOrEmpty(jsonStr)){message.Content = new StringContent(jsonStr, Encoding.UTF8, "application/json");}HttpResponseMessage response = httpClient.SendAsync(message).Result;info = response.Content.ReadAsStringAsync().Result;}catch (Exception e){log.Error(string.Format("请求POST接口失败,URL={0},Error={1},Param={2}", url, JsonConvert.SerializeObject(e), jsonStr));}return info;
}

引用:https://blog.csdn.net/u013667796/article/details/130442415

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com