C#:https文件下载

今天在.net4.5环境下用WebClient下载https的文件时,报错: 基础连接已经关闭: 发送时发生错误。

加上ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;后正常。

using (WebClient client = new WebClient())
{
  ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  client.DownloadFile(url, fullName);
}

 先记录一下;以后遇到其他情况再加上吧。

 附上ServicePointManager.SecurityProtocol Property参考:

ServicePointManager.SecurityProtocol Property (System.Net) | Microsoft Learn

 

如果您已经设置了TLS 1.2协议并且仍然遇到问题,那么可能是由于服务器证书不受信任或已过期。您可以尝试使用以下代码来忽略证书错误:

ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;

 

posted @   新*  阅读(510)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异

喜欢请打赏

扫描二维码打赏

支付宝打赏

点击右上角即可分享
微信分享提示