Skip to content

Instantly share code, notes, and snippets.

@iamsunny
Created February 6, 2019 06:27
Show Gist options
  • Save iamsunny/8b7eeb0522b8cf8be0111b0c7764c403 to your computer and use it in GitHub Desktop.
Save iamsunny/8b7eeb0522b8cf8be0111b0c7764c403 to your computer and use it in GitHub Desktop.
Download files using HttpClient in .NET Core
var urlsFilePath = @"file.txt"; // file contains a tab separated filename/fileUrl combination on each line
var lines = File.ReadAllLines(urlsFilePath);
var outFileFolder = "images";
var httpClient = new HttpClient();
foreach (var line in lines)
{
var fileName = line.Split('\t')[0];
var filePath = line.Split('\t')[1];
try
{
Console.WriteLine($"Downloading: {fileName}");
var response = httpClient.GetAsync(filePath).Result;
var outFilePath = Path.Combine(outFileFolder, fileName);
response.Content.ReadAsFileAsync(outFilePath, true);
Console.WriteLine($"Downloaded: {fileName} - {filePath}");
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
}
Console.WriteLine("Completed");
Console.ReadLine();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment