C# &引用;System.Net.WebException:';请求被中止:无法创建SSL/TLS安全通道。”;控制台内应用程序
我正在开发一种下载html网页的简单方法:C# &引用;System.Net.WebException:';请求被中止:无法创建SSL/TLS安全通道。”;控制台内应用程序,c#,ssl,console-application,webclient,C#,Ssl,Console Application,Webclient,我正在开发一种下载html网页的简单方法: public string DownloadUsingWebRequest(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.UserAgent = "Mozilla / 5.0(Windows NT 10.0
public string DownloadUsingWebRequest(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.UserAgent = "Mozilla / 5.0(Windows NT 10.0; Win64; x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 70.0.3538.77 Safari / 537.36";
request.ServicePoint.Expect100Continue = true;
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 |
SecurityProtocolType.Tls11;
ServicePointManager.ServerCertificateValidationCallback =
(a, b, c, d) => true;
using (var response = (HttpWebResponse)(request.GetResponse()))
{
var code = response.StatusCode;
if (code != HttpStatusCode.OK) return "";
using (var sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return sr.ReadToEnd();
}
}
}
但是,当我运行此代码段并传递https url时,会引发以下异常:
System.Net.WebException: 'The request was aborted: Could not create SSL/TLS secure channel.'
我已经调查过了,但帮不了什么忙
注意:我用作测试的网站使用TLS 1.2。尝试将
ServicePointManager
内容移动到方法的顶部。如果您使用的是Windows 10,则不需要SecurityProtocolType.Tls12
,系统默认值已为该值。如果您在Windows 7或Windows Server 2008上,如果-在您创建连接之前移动了ServicePointManager.SecurityProtocol
=SecurityProtocolType.Tls12
,则会得到相同的结果,这是因为您的系统没有带AES\u 128\u GCM\u SHA256的TLS\u ECDHE\u RSA\u密码套件。越来越多的网站使用这种密码(而且只有这一种——可能是同一套密码的伴侣)。因此,没有运气(除非你带来这个密码套件并管理它)。@rfm我尝试将ServicePointManager
调用移动到方法的开头,同样的异常是thrown@Jimi我在我的测试网站上做了一些研究,它实际上使用了提到的密码套件,这是否意味着我无法读取它的html文本?您可以尝试使用无头网络浏览器。