0

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?

SilverLight
  • 19,668
  • 65
  • 192
  • 300
  • Don't forget to include `SecurityProtocolType.Tls13` I would be tempted to remove SSL3 and TLS versions 1.0 and 1.1 as well. – phuzi Oct 11 '21 at 13:53
  • For [this url](https://blockchain.info/ticker) i have no error. but for [this url](https://api.coindesk.com/v1/bpi/currentprice.json) i face error. I do n't know what is wrong about second url. – SilverLight Oct 11 '21 at 13:54
  • @phuzi Error : 'SecurityProtocolType' does not contain a definition for 'Tls13' – SilverLight Oct 11 '21 at 13:56
  • Sorry, TLS 1.3 is only supported in .Net Framework 4.8 oops – phuzi Oct 11 '21 at 14:00
  • Looking at [SSL Labs analysis of api.coindesk.com](https://www.ssllabs.com/ssltest/analyze.html?d=api.coindesk.com) only TLS 1.2 & 1.3 are supported. What version of Windows are you running? – phuzi Oct 11 '21 at 14:05
  • [blockchain.info appears to support TLS1.1](https://www.ssllabs.com/ssltest/analyze.html?d=blockchain.info&latest) which is no longer supported. – phuzi Oct 11 '21 at 14:08
  • @Selvin OP did acknowledge that the cert was updated today. – phuzi Oct 11 '21 at 14:10
  • What version of Windows are you running? Windows server 2008 - r2 & Windows 7. "only TLS 1.2 & 1.3 are supported" > So why it does not work with TLS 1.2? – SilverLight Oct 11 '21 at 14:16
  • "Windows server 2008 - r2 & Windows 7" oh dear, these versions don't support TLS 1.2 by default. There are ways to enable TLS 1.2 on these platforms though even though they are no longer supported my MS – phuzi Oct 11 '21 at 14:20
  • @phuzi Thanks for comments - Put your way to enable TLS 1.2 on windows 7 as answer. – SilverLight Oct 11 '21 at 14:22
  • @phuzi How could you figure out from [this link](https://www.ssllabs.com/ssltest/analyze.html?d=api.coindesk.com) that only TLS 1.2 & 1.3 are supported? – SilverLight Oct 11 '21 at 14:24
  • If you click through one of the IP Addresses there's a Configuration section that lists supported SSL and TLS protocols. – phuzi Oct 11 '21 at 14:26

1 Answers1

1

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.

phuzi
  • 12,078
  • 3
  • 26
  • 50