Wait for page load before downloading with WebClient

I have several URLs stored in a text file, each of them is a link leading to a Facebook emoji, like https://www.facebook.com/images/emoji.php/v5/u75/1/16/1f618.png

I'm trying to download these images and store them on my disk. I'm using WebClient with DownloadFileAsync, something like

using (var client = new WebClient())  
{
    client.DownloadFileAsync(imgURL, imgName);
}

My problem is even if the amount of URLs is small, say 10, some of the images are downloaded ok, some give me a file corrupt error. So I thought I needed to wait for files to be downloaded till the end and added DownloadFileCompleted event, like this

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Net;

class Program
{
    static Queue<string> q;

    static void Main(string[] args)
    {
        q = new Queue<string>(new[] {
            "https://www.facebook.com/images/emoji.php/v5/u51/1/16/1f603.png",
            "https://www.facebook.com/images/emoji.php/v5/ud2/1/16/1f604.png",
            "https://www.facebook.com/images/emoji.php/v5/ud4/1/16/1f606.png",
            "https://www.facebook.com/images/emoji.php/v5/u57/1/16/1f609.png",
            "https://www.facebook.com/images/emoji.php/v5/u7f/1/16/1f60a.png",
            "https://www.facebook.com/images/emoji.php/v5/ufb/1/16/263a.png",
            "https://www.facebook.com/images/emoji.php/v5/u81/1/16/1f60c.png",
            "https://www.facebook.com/images/emoji.php/v5/u2/1/16/1f60d.png",
            "https://www.facebook.com/images/emoji.php/v5/u75/1/16/1f618.png",
            "https://www.facebook.com/images/emoji.php/v5/u1e/1/16/1f61a.png"
        });
        DownloadItem();
        Console.WriteLine("Hit return after 'finished' has appeared...");
        Console.ReadLine();
    }

    private static void DownloadItem()
    {        
        if (q.Any())
        {
            var uri = new Uri(q.Dequeue());
            var file = uri.Segments.Last();

            var webClient = new WebClient();
            webClient.DownloadFileCompleted += DownloadFileCompleted;
            webClient.DownloadFileAsync(uri, file);
        }
        else 
        {
            Console.WriteLine("finished");
        }
    }

    private static void DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        DownloadItem();
    }
}

It didn't help and I decided to look closer into the files that are corrupted.

It appeared that the files that were corrupted were not actually image files, but HTML pages, which either had some redirection JavaScript code to an image or were full HTML pages saying that my browser was not supported.

So my question is, how do I actually wait that an image file has been fully loaded and is ready to be downloaded?

EDIT I have also tried to remove the using statement, but that did not help either.

Jon Skeet
people
quotationmark

Nothing's being corrupted by your download - it's simply Facebook deciding (sometimes, which is odd) that it doesn't want to serve the image to your client.

It looks like it's the lack of a user agent that causes the problem. All you need to do is specify the user agent, and that looks like it fixes it:

webClient.Headers.Add(HttpRequestHeader.UserAgent,
    "Mozilla/5.0 (compatible; http://example.org/)");

people

See more on this question at Stackoverflow