1

I have a very simple process that I am trying to figure out how to use Task.WhenAll to download multiple pages using HttpClient, but if any of the urls throws an exception, even the one that succeeded dont seem to give me the content page.

  1. Take a list of urls
  2. Use HttpClient .GetStringAsync
  3. Process the ones that succeeded
  4. Get list of all the urls that failed (this might make things messy, if so this is not 100% a requirement)

Here is my code, how can i make it so that if any fail, the remaining will be in pages

urls is List<string> contains list of urls

try
{
    var tasks = urls.Select(x => httpClient.GetStringAsync(x));
    var pages = await Task.WhenAll(tasks);

    //pages will be an array of all the ones that got download without errors

}
catch (Exception e)
{
    //not sure how to properly get the ones that failed
    Console.WriteLine(e);

}
Zoinky
  • 4,083
  • 11
  • 40
  • 78
  • https://stackoverflow.com/a/55888744/34092 – mjwills May 14 '20 at 08:58
  • You could start by combining each url with its associated task in a custom class or [`Tuple`](https://learn.microsoft.com/en-us/dotnet/api/system.tuple-2). Then after awaiting all the tasks you could filter the urls that have their task faulted: `Where(entry => entry.Task.IsFaulted).Select(entry => entry.Url)`. – Theodor Zoulias May 14 '20 at 13:04

0 Answers0