Created
February 6, 2019 06:27
-
-
Save iamsunny/8b7eeb0522b8cf8be0111b0c7764c403 to your computer and use it in GitHub Desktop.
Download files using HttpClient in .NET Core
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
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