尝试从https URL(https://nvd.nist.gov/download/nvd-rss.xml)下载xml文件
该URL可通过浏览器公开访问。
在控制台项目中使用C#Webclient。
但是获取异常如下
using (WebClient client = new WebClient())
{
System.Net.ServicePointManager.SecurityProtocol =
System.Net.SecurityProtocolType.Ssl3;
client.DownloadFile(uri, @"c:\test\nvd-rss.xml");
}
$ exception {“基础连接已关闭:发送中发生意外错误。”} System.Net.WebException
尝试将所有属性(如SSL等)添加到system.Net,但没有帮助。
原因是相关站点仅支持TLS 1.2。在.NET中,默认值System.Net.ServicePointManager.SecurityProtocol
是Ssl
| Tls
,这意味着.NET客户端默认情况下不支持Tls 1.2(在SSL协商期间,它不在支持的协议列表中列出该协议)。至少对于许多.NET Framework版本都是这种情况,不确定是否全部适用。但是.NET实际上确实支持TLS 1.2,要启用它,您应该这样做:
string uri = "https://nvd.nist.gov/download/nvd-rss.xml";
using (WebClient client = new WebClient())
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
client.DownloadFile(uri, @"c:\test\nvd-rss.xml");
}
而且你应该没事。当然,最好支持多个TLS 1.2协议,因为System.Net.SecurityProtocolType是全局设置,并且并非所有站点都支持TLS 1.2:
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls12;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句