【问题标题】:Error when making request: The request was aborted: Could not create SSL/TLS secure channel发出请求时出错:请求被中止:无法创建 SSL/TLS 安全通道
【发布时间】:2021-11-30 06:21:36
【问题描述】:

这是我的代码:

try {
    HttpWebRequest req = (HttpWebRequest) WebRequest.Create("https://api.coindesk.com/v1/bpi/currentprice.json");
    req.Method = "GET";
    req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*;q=0.8";
    req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
    req.ContentType = "text/html; charset=utf-8";
    req.Referer = "";
    req.KeepAlive = true;
    req.Timeout = 25000;
    req.AllowAutoRedirect = true;

    CookieContainer cookieJar1 = new CookieContainer();
    req.CookieContainer = cookieJar1;

    HttpWebResponse res = (HttpWebResponse) req.GetResponse();

    foreach(Cookie cookie in res.Cookies) {
        cookieJar1.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), "/", cookie.Domain));
    }

    Stream Stream = res.GetResponseStream();
    StreamReader reader = new StreamReader(Stream);
    string reader_str = reader.ReadToEnd();

    var obj = JObject.Parse(reader_str);
    string bitcoin_price_str = ((string) obj["bpi"]["USD"]["rate"]).Trim().Replace(",", "");
    bitcoin_price = double.Parse(bitcoin_price_str);

    reader.Close();
    Stream.Close();
    res.Close();
}
catch(Exception ex_1) {
    Send_Email_Gmail();
}

在今天之前,这些代码可以正常工作。
但是今天我遇到了一个关于Invalid Certificate Date 的错误。
当我在浏览器(FireFox 和 Chrome)中打开 that url 时,出现了该错误。
几个小时前,他们在他们的服务器上解决了这个问题。
现在在 FireFox 和 Chrome 中没有问题。
但在我的代码中,我遇到了这个错误:

请求被中止:无法创建 SSL/TLS 安全通道。

我试过this solution

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12| SecurityProtocolType.Ssl3;

没有帮助。
我该怎么办?

【问题讨论】:

  • 别忘了包含SecurityProtocolType.Tls13 我也很想删除 SSL3 和 TLS 版本 1.0 和 1.1。
  • 对于this url 我没有错误。但是对于this url,我面临错误。我不知道第二个网址有什么问题。
  • @phuzi 错误:“SecurityProtocolType”不包含“Tls13”的定义
  • 抱歉,TLS 1.3 仅在 .Net Framework 4.8 中受支持,哎呀
  • 查看SSL Labs analysis of api.coindesk.com 仅支持 TLS 1.2 和 1.3。您运行的是哪个版本的 Windows?

标签: c# ssl-certificate windows-7-x64 .net-4.6.2


【解决方案1】:

好像api.colindesk.com now only supports TLS 1.2 and 1.3blockchain.info still supports the deprecated TLS 1.1

这立即(至少对我而言)表明您正在使用默认不支持 TLS1.2 的旧版本的 Windows - 在问题的 cmets 中得到确认。

关于 SO 有很多答案可以说明如何做到这一点。

【讨论】:

    猜你喜欢
    • 2012-06-05
    • 2018-04-22
    • 2012-10-30
    相关资源
    最近更新 更多