Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demonstrating the use of WebClient.DownloadStringAsync to parallelize http requests
class ParallelHttpRequests
{
static void Main()
{
var count = 300;
var root = "http://files.kenegozi.com/temp/";
ServicePointManager.DefaultConnectionLimit = 1000;
var watch = Stopwatch.StartNew();
for (var i = 0; i < count; ++i)
{
var client = new WebClient();
var url = string.Format("{0}file{1:0000}.txt", root, i);
client.DownloadString(url);
}
Console.WriteLine("for loop: " + watch.Elapsed);
watch = Stopwatch.StartNew();
Parallel.For(0, count, i =>
{
var client = new WebClient();
var url = string.Format("{0}file{1:0000}.txt", root, i);
client.DownloadString(url);
});
Console.WriteLine("parallel: " + watch.Elapsed);
watch = Stopwatch.StartNew();
var completed = new CountdownEvent(count);
for (var i = 0; i < count; ++i)
{
var client = new WebClient();
client.DownloadStringCompleted += (s, e) => completed.Signal();
var url = string.Format("{0}file{1:0000}.txt", root, i);
client.DownloadStringAsync(new Uri(url));
}
completed.Wait();
Console.WriteLine("async: " + watch.Elapsed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.