Skip to content

Instantly share code, notes, and snippets.

@yevhen
Created November 13, 2017 13:19
Show Gist options
  • Save yevhen/fa81acdfa42511ee60b1c678b2980889 to your computer and use it in GitHub Desktop.
Save yevhen/fa81acdfa42511ee60b1c678b2980889 to your computer and use it in GitHub Desktop.
readonly HttpClient http = new HttpClient();
async Task<DownloadResult[]> DownloadUrlsAsync(string path) =>
await Task.WhenAll(File.ReadLines(path).Select(DownloadAsync));
async Task<DownloadResult> DownloadAsync(string url)
{
var time = Stopwatch.StartNew();
try
{
return new DownloadResult
{
Url = url,
Time = time.ElapsedMilliseconds,
Success = true,
SizeInBytes = (await http.GetByteArrayAsync(url)).Length
};
}
catch (Exception x)
{
Console.WriteLine("error downloading {0}: {1}", url, x);
return new DownloadResult
{
Url = url,
Time = time.ElapsedMilliseconds,
};
}
}
class DownloadResult
{
public string Url;
public long Time;
public bool Success;
public int SizeInBytes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment