Skip to content

Instantly share code, notes, and snippets.

@DamianEdwards
Created November 7, 2012 09:31
Show Gist options
  • Save DamianEdwards/4030394 to your computer and use it in GitHub Desktop.
Save DamianEdwards/4030394 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();
}
}
}
}
@micahasmith
Copy link

I never knew about HttpTaskAsyncHandler until now. The fact that it allows you to do IsReusable="true" is awesome. Can't wait to benchmark this thing.

Thanks for posting!

@justinmchase
Copy link

Throw some linq in there while you're at it:

while(downloadTasks.Any()) { ... }

:)

@Meligy
Copy link

Meligy commented Feb 20, 2014

Would this work with Response.BufferOutput = false; if you are getting a Stream not a string?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment