Making concurrent HTTP requests using httpclient in .NET C #
I have created a console application that basically does a lot of requests to my server. 10-15000 requests per user.
So, I wrote this code which uses the .NET HTTP Client library:
public async Task<string> DoRequest(string token)
{
var request = new HttpRequestMessage(HttpMethod.Post, "mysite.com");
string requestXML = "my xml request goes her...";
request.Content = new StringContent(requestXML, Encoding.UTF8, "text/xml");
var response = await httpClient.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
So now I'm trying to do the same as HTTP requests I can in 1 second ... I don't know what the upper limit is on this, so I used parallel for a loop to speed this up:
Parallel.For(0,list.Count(),items=>{
DoRequest(item.Token);
});
But I'm not very happy with the speed of the code and the way the requests are made ...
Is there a way to speed things up to maybe 10-100 requests in 1 second? Or what's the maximum limit?
Can anyone help me?
PS guys, I only created one instance of the httpclient class because I read that the only good practice is that every time a new object of that class becomes private, isn't that something we don't want?
source to share
Is there a way to speed things up to maybe 10-100 requests in 1 second?
This is an unanswered question.
Or what's the maximum limit?
As David pointed out, you need to install ServicePointManager.DefaultConnectionLimit
. This should suffice as the first line in your method Main
:
ServicePointManager.DefaultConnectionLimit = int.MaxValue;
I used parallel for loop to speed up this
This is the wrong tool for the job. You really want concurrency, but not parallelism. Asynchronous concurrency is expressed with Task.WhenAll
:
var tasks = list.Select(item => DoRequest(item.Token));
await Task.WhenAll(tasks);
I only created one instance of the httpclient class because I read that it is only a good practice that every time a new object of that class is created, the connection is closed, which is not something we don't want?
It is right.
source to share