error when fetching json file from C# website

titojd

Well-known member
Joined
Oct 12, 2018
Messages
57
Programming Experience
1-3
Hello Colleagues, here I am again asking colleagues for advice.

I have two methods in my application that are giving me a headache,

one reads a json file and saves it in txt and the other prints some results on the screen,
everything was working perfectly until a week ago, and suddenly an error started to appear,
These are the methods:
C#:
private void SaveDetails()
        {
            var url = @"https://servicebus2.caixa.gov.br/portaldeloterias/api/lotofacil";
            var requisition = (HttpWebRequest)WebRequest.Create(url); // save the overall result in txt                                                   
            var resposta = (HttpWebResponse)requisition.GetResponse();

                using (var responseStream = resposta.GetResponseStream())
                {
                    using (var fileStream = new FileStream(Path.Combine(diretorio + "C:\\BoaSorte\\ResultadoAtual.txt"), FileMode.Create))
                    {
                        responseStream.CopyTo(fileStream);
                    }
                }                       
        }

C#:
private void LoadResult()
        {
            var url = @"https://servicebus2.caixa.gov.br/portaldeloterias/api/lotofacil";
            HttpClient lotofacil = new HttpClient();
            var response = lotofacil.GetAsync(url).Result;

                if (response.IsSuccessStatusCode)
                {
                    var listTens = response.Content.ReadAsStringAsync().Result;
                    var list = JsonConvert.DeserializeObject<ResultadoAtual>(listTens);

                    Control[] controles = ResultadoAtual;

                    for (int i = 0; i < controles.Length; i++)
                    {
                        int valor = Convert.ToInt32(list.listaDezenas[i]);
                        controles[i].Text = valor.ToString("D2");
                    }
                    var rateio = list.listaRateioPremio[0];
                    lblGanhadores.Text = rateio.numeroDeGanhadores.ToString("D2");
                    txtConcurso.Text = list.numero.ToString();
                    lblData.Text = list.dataApuracao.ToString();
                    txtDataAnterior.Text = list.numeroConcursoAnterior.ToString();
                    txtDataAtual.Text = list.numero.ToString();
                    txtDataProximo.Text = list.numeroConcursoProximo.ToString();
                    txtProximoConcurso.Text = list.dataProximoConcurso.ToString();
                }                       
        }
this is the error

C#:
ar resposta = (HttpWebResponse)requisition.GetResponse();
An unhandled exception of type 'System.Net.WebException' occurred in System.dll

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

Thank you very much for the forum, a hug.
 
If your code has not changed, and you have not changed your system configuration, then that means that the site you are accessing has updated their system configuration to not accept obsolete crypto protocols.

Now on the other hand, if you have changed your system configuration, then it is possible you are trying to use a modern current crypto protocol, but the site won't accept it.

What version of.NET Framework or .NET are you using? If it is older than .NET 4.6.2, are you setting the (undocumented at that time) flags to use modern TLS and remove the (documented) older TLS? If newer than that, are you sure you are not accidentally setting the flags too use the oldee TLS protocols ? More is not always better.
 
@Skydiver Thanks for responding
I'm using the same version of the .NET Framework 4.5.2, I created Setup.exe in November 2023, until a week ago it worked perfectly.

Using the browser, I can access the updated link page.
 
Yes, because the browser is not using .NET Framework 4.5.2. It is using it's own code to negotiate its TLS channel. .NET Framework 4.5.2 defaults to using on obsolete TLS.
 
Upgrade the machine to .NET Framework 4.8.1. Then recompile your code to target .NET Framework 4.8.1.
 

Latest posts

Back
Top Bottom