我想测试一下我的几个Web服务。 如何并行发送httpWebRequest?
答案 0 :(得分:3)
您是否尝试使用任务并行库。您可以找到更多信息here。
例如,您可以调用Invoke方法并行执行几个委托:
Parallel.Invoke(() => DoSomeWork(), () => DoSomeOtherWork());
答案 1 :(得分:3)
试试这个:
new List<string>
{
"http://www.stackoverflow.com",
"http://www.google.com"
}
.AsParallel().ForAll(x =>
{
var client = new WebClient();
client.DownloadStringAsync(new Uri(x));
client.DownloadStringCompleted +=
(o, e) =>
{
var result = e.Result; // html will be here
Console.WriteLine("Completed");
};
});
或者这个:
Parallel.ForEach(new List<string>
{
"http://www.stackoverflow.com",
"http://www.google.com"
}, x =>
{
var client = new WebClient();
client.DownloadStringAsync(new Uri(x));
client.DownloadStringCompleted +=
(o, e) =>
{
var result = e.Result; // html will be here
Console.WriteLine("Completed");
};
}
有关详细信息,请参阅Parallel Programming