0

As Suggested by @John Wu's Answer on Stackoverflow. I am using the following code to make concurrent HTTP Get Requests on the same host for sending SMS

    public async Task SendBulk(List<string> recipentURLs)
    {
        using (var client = new HttpClient())
        {
            //Start requests for all of them
            var requests = recipentURLs.Select(url => client.GetAsync(url)).ToList();

            //Wait for all the requests to finish
            await Task.WhenAll(requests);
        }
    }

Now I am having the exception saying

System.Net.Http.HttpRequestException: An error occurred while sending the request.
 ---> System.IO.IOException: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond..
 ---> System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

The API I am sending requests to doesn't require a response (as I am only using it to send sms).

I am using this in a backgroud job by using HangFire. So I am okay if it takes a long time.

What I am currently looking is an efficient way to send multiple HTTP Get Requests at the Same host.

How can I safely handle all these concurrent requests and retry if any of it fails?

Also I am currently researching that due HTTP Throttling I cannot use 2 connection to the same host?

Hamza Khanzada
  • 1,439
  • 1
  • 22
  • 39
  • Every request gets an acknowledgement even if it is just a status of 200 OK. How do you know when the task complete (await Task.WhenAll(requests);) without a response. What type of failure do you expect? A connection failed, credentials failed, the sms was bad, or the sender was bad? – jdweng Apr 16 '20 at 13:34
  • I just want all of my requests to have a 200 Status OK code response. Nothing like `result.Content.ReadAsStringAsync()` I am currently having `System.Net.Sockets.SocketException (10060)` exception on `await Task.WhenAll(requests);` and I dont know what approach to use to fix this exception? – Hamza Khanzada Apr 16 '20 at 13:47
  • 1
    The Service may not be running on the SERVER or it could be blocked. Not sure if you all are failing or just some. I would use a sniffer like wireshark or fiddler and determine is any are working correctly. Some servers only allow one connection at a time from a client (same IP address) to prevent service attacks. – jdweng Apr 16 '20 at 14:10
  • Just some of the http requests are failing, and if its a case of `Some servers only allow one connection at a time from a client (same IP address) to prevent service attacks` then what is the best approach to use one connection and send httprequests so that all the requests are processed successfully (I dont care about how much time it would take) – Hamza Khanzada Apr 16 '20 at 14:32
  • Yes. Better to send one at a time so you can retry when a failure occurs. – jdweng Apr 16 '20 at 14:47

1 Answers1

1

Closed HttpClient instances leave sockets open in the TIME_WAIT state for a short period of time. If a code path that creates and disposes of HttpClient objects is frequently used, the app may exhaust available sockets. You should use HttpClientFactory to avoid that. See the post Make HTTP requests using IHttpClientFactory in ASP.NET Core.

loick
  • 27
  • 2