简体   繁体   中英

C# HttpClient Multiple Get Requests at the same time giving System.Net.Sockets.SocketException (10060) Exception

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM