0

i made a program, to retrieve some price from some website, it works pretty good, but if i let my application rest or idle for 1 or 2 minutes then when i want to run a price check againm it would throw an exception "an error occurred while sending the request" , i have tried everything i found in google, like adding SecurityProtocol, running it on a Task or using WebClient, but nothing works.

here is the function that retrieve my http code, i then extract the price from it.

 HttpClient client = new HttpClient();

 public async Task ListViewScanner(string URL, int SelectedItem)
        {
            string webData;
            try
            {
                System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 |
                                                    System.Net.SecurityProtocolType.Tls11 |
                                                    System.Net.SecurityProtocolType.Tls;
                using (HttpResponseMessage response = await client.GetAsync(URL))
                {

                    response.EnsureSuccessStatusCode();
                    webData = await response.Content.ReadAsStringAsync(); //HERE MY CATCH GETS TRIGGERED
                }
            }
            catch (HttpRequestException e)
            {
                MessageBox.Show($"Message :{e.Message} ");
                webData = "ERROR";
            }
         }

I commented where i get the exception.

exceptions

    SocketException: An existing connection was forcibly closed by the remote host

IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

WebException: The underlying connection was closed: An unexpected error occurred on a send.

HttpRequestException: An error occurred while sending the request.
  • You should provide the full exception details, including any inner exceptions. If you're request to the same location is working the few times and failing later, then TLS most likely isn't the issue. Also, critical information for anyone using HttpClient is [You're using HttpClient Wrong And It's Destabilizing Your Software](https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/). Due to the pain that HttpClient is, I tend to use [Flurl](https://flurl.dev/) which takes care of the painful parts for you, and gives a much cleaner syntax. – mason Aug 05 '21 at 15:09
  • `ServicePointManager.SecurityProtocol` line not needed if you're running on .NET Framework 4.6.1 or newer. If you're running .NET Core 3/.NET 5 - it has no effect. Read `response.StatusCode`/`response.ReasonPhrase` before ensuring its success line e.g. `MessageBox.Show($"{response.StatusCode} {response.ReasonPhrase}");` then EnsureSuccess line... You may also remove `response.EnsureSuccessStatusCode();` to view the `Content` in case of not success response. – aepot Aug 05 '21 at 16:55
  • hi ill update my post, to add the inner exceptions, sorry. Btw, i tried Flurl and still same results. – Alejandro Sánchez Aug 05 '21 at 17:09
  • See this question: https://stackoverflow.com/questions/2582036/an-existing-connection-was-forcibly-closed-by-the-remote-host – vakio Aug 05 '21 at 17:13
  • hi i tried all mentioned there still have the problem, i found out using a timer, that i can use my application all i want, if i keep requesting from the httpclient, the problem comes when i let the program idle for 2 minutes. – Alejandro Sánchez Aug 05 '21 at 21:38

1 Answers1

0

Use a new instance of HttpClient every time, it's maybe related to the old instance that close the connection.

don't forge to use with a using statement to make sure that is destroyed when the call end.

Houssem
  • 1,042
  • 7
  • 16
  • hi, i already tried creating the instance everytime im checking the prices, but i still get the errors, this is why it seems so weird to me, even creating a new instance of httpclient would give me exception after 1 or 2 minutes – Alejandro Sánchez Aug 05 '21 at 20:18
  • can i know what type of app is ? do you run over a azure web app / functions ? or is a console app ? – Houssem Aug 06 '21 at 16:44
  • hi this is a winform, and the URL im triying to download its data, its a public website, today im out of ideas now. – Alejandro Sánchez Aug 06 '21 at 20:41
  • You can try to make a retry system , otheriwse we will need a sample so we can help , because this can be related to the website that you consumee or to the infrastructure. – Houssem Aug 07 '21 at 12:07
  • can you please elaborate more about the retry system ? thanks. – Alejandro Sánchez Aug 08 '21 at 15:23
  • also, i just measure the time it takes to throw exception, if i let httpclient/webclient/webrequest ide for 1:30 minute, then i will get an exception, if i keep running it before that time pass, then i can use it undefinedly – Alejandro Sánchez Aug 20 '21 at 23:44
  • It's a timeout , i don't know how i passed this , is a know issue , the default timeout is 100s so it's clear now the reason , try to set the timeout to another value example `client.Timeout=TimeSpan.FromMinutes(5)` // this will give you 5minutes of timeout. – Houssem Aug 23 '21 at 12:53
  • hello, still getting the exception, but looks like the host is closing the connection, if i try another website, then it works forever, but the weird thing is i can run it off a timer all the time i want, the problem comes when it ide – Alejandro Sánchez Sep 03 '21 at 15:20
  • found a temporary fix, what i did was make a fake request to another URL before my real request, not what i like, but its something – Alejandro Sánchez Sep 03 '21 at 15:35