简体   繁体   English

在.NET C#中使用httpclient同时执行http请求

[英]Performing concurrently http requests using httpclient in .NET C#

I have made a console application which basically performs large amount of requests towards my server. 我已经制作了一个控制台应用程序,它基本上对我的服务器执行大量请求。 10-15000 requests per user. 每个用户10-15000个请求。

So I've written this code which uses .NET's HTTP Client library: 所以我编写了这个使用.NET的HTTP客户端库的代码:

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 perform as much as HTTP requests that I can per 1 second... I don't know whats the upper limit of this so I've used parallel for loop to speed this: 所以现在我正在尝试执行尽可能每1秒的HTTP请求...我不知道这是什么的上限所以我使用并行for循环来加速这个:

Parallel.For(0,list.Count(),items=>{
DoRequest(item.Token);
});

But I'm not very happy with the speed of the code and how the requests are being made... 但我对代码的速度以及请求的制作方式不满意......

Is there any way that I can speed things up n do maybe up to 10-100 requests per 1 second ? 有什么方法可以加快速度吗?每1秒可能有多达10-100个请求吗? Or what is the maximum limit ? 或者最大限度是多少?

Can someone help me out ? 有人可以帮我吗 ?

PS guys I created only one instance of httpclient class because I read that it's a good practice to make only one since each time a new object of this class is made the connection gets closed, which is not what we want no ? PS家伙我只创建了一个httpclient类的实例,因为我读到这是一个很好的做法,因为每次创建这个类的新对象时连接都会被关闭,这不是我们想要的吗?

Is there any way that I can speed things up n do maybe up to 10-100 requests per 1 second? 有什么方法可以加快速度吗?每1秒可能有多达10-100个请求吗?

That's an unanswerable question. 这是一个无法回答的问题。

Or what is the maximum limit? 或者最大限度是多少?

As David noted, you need to set ServicePointManager.DefaultConnectionLimit . 正如David所说,您需要设置ServicePointManager.DefaultConnectionLimit This should suffice as the first line in your Main method: 这应该足够作为Main方法的第一行:

ServicePointManager.DefaultConnectionLimit = int.MaxValue;

I've used parallel for loop to speed this 我用并行for循环来加速这个

That's the incorrect tool for this job. 这是这项工作不正确的工具。 You do want concurrency, but not parallelism. 你确实需要并发,但不是并行。 Asynchronous concurrency is expressed using Task.WhenAll : 使用Task.WhenAll表示异步并发:

var tasks = list.Select(item => DoRequest(item.Token));
await Task.WhenAll(tasks);

I created only one instance of httpclient class because I read that it's a good practice to make only one since each time a new object of this class is made the connection gets closed, which is not what we want no? 我只创建了一个httpclient类的实例,因为我读到这是一个很好的做法,只有一个,因为每次这个类的新对象被连接关闭,这不是我们想要的不是吗?

That is correct. 那是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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