Created
November 7, 2012 09:31
-
-
Save DamianEdwards/4030394 to your computer and use it in GitHub Desktop.
Async site scraping in ASP.NET 4.5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} | |
} |
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
Throw some linq in there while you're at it:
while(downloadTasks.Any()) { ... }
:)