Created
July 23, 2015 14:24
-
-
Save jlennox/b73b531446e517c1bb44 to your computer and use it in GitHub Desktop.
This file contains 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
Task AnarcodesWebClientLoop(string[] urls) { | |
using (var entryGate = new SemaphoreSlim(10, 10)) { | |
var requests = urls.Select(async url => { | |
await entryGate.WaitAsync(); | |
var didError = false; | |
string errorMessage = null; | |
try { | |
var req = WebRequest.Create(url); | |
using (var res = (HttpWebResponse)await req.GetResponseAsync()) | |
{ | |
didError = (int)res.StatusCode < 200 || (int)res.StatusCode >= 300); | |
using (var reader = new StreamReader(res.GetResponseStream())) | |
errorMessage = await reader.ReadToEndAsync(); | |
} | |
} catch (Exception e) { | |
didError = true; | |
if (errorMessage == null) | |
errorMessage = e.Message; | |
} | |
// do things with didError and errorMessage here. | |
}).ToArray(); | |
return Task.WhenAll(requests); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment