2

WebClient DownloadFileAsync() does not work with the same URl and Credentials...

Any clue?

 static void Main(string[] args)
        {
            try
            {
                var urlAddress = "http://mywebsite.com/msexceldoc.xlsx";


                using (var client = new WebClient())
                {
                    client.Credentials = new NetworkCredential("UserName", "Password");
                    // It works fine.  
                    client.DownloadFile(urlAddress, @"D:\1.xlsx");
                }

                /*using (var client = new WebClient())
                {
                   client.Credentials = new NetworkCredential("UserName", "Password");

                    // It y creats file with 0 bytes. Dunow why is it. 
                    client.DownloadFileAsync(new Uri(urlAddress), @"D:\1.xlsx");
                    //client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);

                }*/
            }
            catch (Exception ex)
            {

            }
        }
NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 5
    If that is literally the contents of your program, it's because `Main` is exiting before the event is raised. When Main exits, the process terminates. You either need to use the synchronous version as you are, or block `Main` from exiting until the event is raised. – vcsjones Sep 07 '16 at 13:30
  • How to stop main from exiting if coded in powershell? – Zimba May 29 '20 at 17:44

2 Answers2

6

You need to keep the program running while the async download completes, as it runs in another thread.

Try something like this, and wait for it to say completed before you hit enter to end the program:

static void Main(string[] args)
    {
        try
        {
            var urlAddress = "http://mywebsite.com/msexceldoc.xlsx";

            using (var client = new WebClient())
            {
               client.Credentials = new NetworkCredential("UserName", "Password");

                client.DownloadFileAsync(new Uri(urlAddress), @"D:\1.xlsx");
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
        }
        catch (Exception ex)
        {

        }

    Console.ReadLine();
    }

public static void Completed(object o, AsyncCompletedEventArgs args)
{
    Console.WriteLine("Completed");
}

Depending what kind of app you're using this in, the main thread needs to keep running while the background thread downloads the file.

Simon
  • 1,081
  • 9
  • 14
3

By declaring Main function as async, you can also use DownloadFileTaskAsync with await.

public static async void Main(string[] args)
{
    var urlAddress = "http://mywebsite.com/msexceldoc.xlsx";
    var fileName = @"D:\1.xlsx";

    using (var client = new WebClient())
    {
        await client.DownloadFileTaskAsync(new Uri(urlAddress), fileName);
    }
}
Halis S.
  • 446
  • 2
  • 11
  • What's the advantage of this? – Paul Dec 10 '17 at 22:44
  • By using `async` `await`, you will not have to suspend main function with `Console.Readline()`. If you prefer not to use `async-await`, you can call like `client.DownloadFileTaskAsync().Result` to force wait the main function. – Halis S. Dec 11 '17 at 07:38