如何在短时间内调用大量http请求
本文关键字:http 请求 调用 短时间 | 更新日期: 2023-09-27 18:10:47
我必须每隔几秒钟发送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是非常慢的!
确保你已经设置了:
webReq.Proxy = null;
似乎WebRequest的启动将寻找默认的代理