解决“The remote certificate is invalid according to the validation procedure”问题

在用HttpClient发起https请求时,遭遇了“The remote certificate is invalid according to the validation procedure”异常,异常信息如下:

复制代码
Exception Details: System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.

[AuthenticationException: The remote certificate is invalid according to the validation procedure.]
   System.Net.TlsStream.EndWrite(IAsyncResult asyncResult) +8622023
   System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar) +225

[WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.]
   System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context) +3564749
   System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar) +155
复制代码

HttpClient调用代码如下:

var httpClient = new HttpClient();
var httpContent = new FormUrlEncodedContent(new 
Dictionary<string, string>
{
    {"grant_type", "client_credentials"}
});
var response = await httpClient.PostAsync("https://...", httpContent);

解决方法:

在web.config中添加如下的配置:

<system.net>
  <settings>
    <servicePointManager checkCertificateName="false" checkCertificateRevocationList="false" />
  </settings>
</system.net>

 

posted @   dudu  阅读(14398)  评论(5编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
历史上的今天:
2013-02-03 [IIS][ASP.NET]“拒绝访问临时目录”的解决方法
2012-02-03 浏览器兼容CSS代码:按钮文字垂直居中(input button text vertical align)
2005-02-03 可以通过二级域名访问团队Blog
2005-02-03 使用SPS时发现的一些问题
2005-02-03 遭遇MSN病毒W32.Bropia
2004-02-03 [转帖]ASP.NET服务器端异步Web方法
2004-02-03 SQL Server唯一约束的使用
点击右上角即可分享
微信分享提示