发出请求时出错:请求被中止:无法创建 SSL/TLS 安全通道
Error when making request: The request was aborted: Could not create SSL/TLS secure channel
这是我的代码:
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 中没有问题。
但是在我的代码中我遇到了这个错误:
The request was aborted: Could not create SSL/TLS secure channel.
我试过了 this solution :
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12| SecurityProtocolType.Ssl3;
没有帮助。
我该怎么办?
好像api.colindesk.com now only supports TLS 1.2 and 1.3
而 blockchain.info still supports the deprecated TLS 1.1.
这立即(至少对我而言)表明您正在使用默认不支持 TLS1.2 的 Windows 旧版本 - 在问题的评论中得到确认。
SO 上有许多答案说明了如何执行此操作。
- .Net Framework 4.6.1 not defaulting to TLS 1.2
- WebService using TLS1.1, TLS1.2 unable to connect on development server, works in local development
- TLS 1.2 error Could not create SSL/TLS secure channel
这是我的代码:
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 中没有问题。
但是在我的代码中我遇到了这个错误:
The request was aborted: Could not create SSL/TLS secure channel.
我试过了 this solution :
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12| SecurityProtocolType.Ssl3;
没有帮助。
我该怎么办?
好像api.colindesk.com now only supports TLS 1.2 and 1.3 而 blockchain.info still supports the deprecated TLS 1.1.
这立即(至少对我而言)表明您正在使用默认不支持 TLS1.2 的 Windows 旧版本 - 在问题的评论中得到确认。
SO 上有许多答案说明了如何执行此操作。
- .Net Framework 4.6.1 not defaulting to TLS 1.2
- WebService using TLS1.1, TLS1.2 unable to connect on development server, works in local development
- TLS 1.2 error Could not create SSL/TLS secure channel