Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Forked from DamianEdwards/gist:4030394
Created November 7, 2012 10:06
Show Gist options
  • Save tugberkugurlu/4030617 to your computer and use it in GitHub Desktop.
Save tugberkugurlu/4030617 to your computer and use it in GitHub Desktop.
Async site scraping in ASP.NET 4.5
public class SiteScrape : HttpTaskAsyncHandler
{
public override async Task ProcessRequestAsync(HttpContext context)
{
using (var http = new HttpClient())
{
var downloadTasks = new List<Task<string>> {
http.GetStringAsync("http://bing.com"),
http.GetStringAsync("http://google.com"),
http.GetStringAsync("http://oredev.org"),
http.GetStringAsync("http://microsoft.com"),
http.GetStringAsync("http://asp.net"),
http.GetStringAsync("http://signalr.net")
};
while (downloadTasks.Count > 0)
{
var completed = await Task.WhenAny(downloadTasks);
downloadTasks.Remove(completed);
context.Response.Write(completed.Result);
await context.Response.FlushAsync();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment