How to find out which of these sites failed

Viewed 93

I have an asyncronous task that goes out to several websites parses some data and returns the data when its finished. In the event that one or more of the websites fail - I need to find out which ones failed. Is this possible? Here is some example code I have:

The controller

public async Task<ActionResult> Index()
{
    Models.WebSite ws = new Models.WebSite();
    List<Task<string>> webList = new List<Task<string>>();
    webList.Add(ws.GetWebsiteInfoAsync("http://website1.com"));
    webList.Add(ws.GetWebsiteInfoAsync("http://website2.com"));
    webList.Add(ws.GetWebsiteInfoAsync("http://website3.com"));

    var taskComplete = await Task.WhenAll(webList);
    return View(taskComplete);
}

The class that makes the web requests:

public async Task<string> GetWebsiteInfoAsync(string url)
{
    System.Net.WebClient wc = new System.Net.WebClient();
    string result = null;
    result = await wc.DownloadStringTaskAsync(new Uri(url));
    return result;
}
2 Answers
Related