Error when making request: The request was aborted: Could not create SSL/TLS secure channel

634 Views Asked by At

Here is my codes :

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();
}

Before today those codes were working ok.
But today i faced an error about Invalid Certificate Date.
When i opened that url in browser (FireFox & Chrome) that error was there.
Some hours ago they fixed the issue on their server.
Now in FireFox & Chrome there is no problem.
But in my codes i face this error :

The request was aborted: Could not create SSL/TLS secure channel.

I tried this solution :

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

Not help.
What should i do?

1

There are 1 best solutions below

0
phuzi On BEST ANSWER

It seems that api.colindesk.com now only supports TLS 1.2 and 1.3 while blockchain.info still supports the deprecated TLS 1.1.

This immediately (to me at least) suggests that you're using old versions of Windows that don't support TLS1.2 by default - confirmed in the comments on the question.

There are numerous answers on SO that show how to do this.