gpt4 book ai didi

c# - 如何在短时间内调用大量http请求

转载 作者:行者123 更新时间:2023-11-30 18:23:25 25 4
gpt4 key购买 nike

我必须每隔几秒发送 100,000 个 http 请求,使用下面提供的代码,发送请求需要 19 秒,比我的间隔时间还长。

这段时间CPU使用率和以太网使用率都是100%。我在带宽更高的双处理器计算机上尝试过,但结果相同。

有没有其他方法可以有更好的性能。

protected async void btnStartCallWhenAll_Click(object sender, EventArgs e)
{
t1 = DateTime.Now;
// Make a list of web addresses.
List<string> urlList = SetUpURLList(Convert.ToInt32(txtNoRecordsToAdd.Text));

// One-step async call.
await ProcessAllURLSAsync(urlList);

t2 = DateTime.Now;
double spent = t2.Subtract(t1).TotalMilliseconds;
txtTimeElapsed.Text = " Time Spent :" + spent.ToString() ;
}
private List<string> SetUpURLList(int No)
{
List<string> urls = new List<string>
{
};

for (int i = 1; i <= No; i++)
urls.Add("http://msdn.microsoft.com/library/windows/apps/br211380.aspx");

return urls;
}
private async Task ProcessAllURLSAsync(List<string> urlList)
{
ServicePointManager.UseNagleAlgorithm = true;
ServicePointManager.Expect100Continue = true;
ServicePointManager.CheckCertificateRevocationList = true;
ServicePointManager.MaxServicePointIdleTime = 10000;
ServicePointManager.DefaultConnectionLimit = 1000;

IEnumerable<Task> CallingTasksQuery =
from url in urlList select CallURLAsync(url);

Task[] CallingTasks = CallingTasksQuery.ToArray();

await Task.WhenAll(CallingTasks);
}

private async Task CallURLAsync(string url)
{
var content = new MemoryStream();
var webReq = (HttpWebRequest)WebRequest.Create(url);
using (WebResponse response = await webReq.GetResponseAsync())
{
}
}

最佳答案

引用HttpWebRequest is extremely slow!

确保您已设置:

webReq.Proxy = null;

看起来好像 WebRequest 的启动会寻找默认代理。

关于c# - 如何在短时间内调用大量http请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32335116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com