Is it possible to make multiple concurrent requests with httpclient?

Is it possible to make multiple concurrent requests with httpclient?

It’s thread-safe and can handle multiple requests. You can fire off multiple requests from the same thread and await all of the responses, or fire off requests from multiple threads. No matter what the scenario, HttpClient was built to handle concurrent requests. To use HttpClient effectively for concurrent requests, there are a few guidelines:

How to await multiple HTTP requests in C #?

In that case you would want to create the tasks and then wait for them both: var responseTask1 = httpClient.SendAsync(request1); var responseTask2 = httpClient.SendAsync(request2); await Task.WhenAll(responseTask1, responseTask2); responseMessages.AddRange(new [] { responseTask1.Result, responseTask2.Result }); return responseMessages;

How to await and return the result of a HTTP request?

However, it is unclear to me, how to wait for the response object for further processing and how to return it as result of doRequest ():

When to wait for multiple requests to finish?

The scenario: you want to make multiple requests at the same time, and wait for them all to finish before returning all the data. Or, alternatively, you don’t need to return any data but instead just need them all to execute before the function returns. Maybe you’re looking to batch similar requests into X number at a time.

How to send multiple requests at the same time?

I’m trying to create a script that send’s over 1000 requests to one page at the same time. But requests library with threading (1000) threads. Seems to be doing to first 50 or so requests all within 1 second, whereas the other 9950 are taking considerably longer.

How to make multiple Ajax requests with one callback?

In our simple use case, we can use jQuery’s $.when () method, which takes a list of these “Deferred” objects (All jQuery Ajax methods return Deferred objects) and then provides a single callback. So our callback-hell can be rewritten like:

What happens when there are more than 4 sockets open in httpclient?

But here’s what really happens: There are more than four sockets open at once, and HttpClient will keep opening new sockets as it processes requests. In other words, when things are going right, it’ll cap the number of sockets it allocates based on the max concurrency you specified.