Manoj Savalia Manoj Savalia - 1 month ago 16
C# Question

How can I download a ZIP file from a URL using C#?

I want to download a ZIP file from some web URL.
When I open the browser and write the URL, the browser directly start downloading the ZIP file. However what I want is to automate this using C# code.

I have tried the following code:

private void btnDownload_Click(object sender, EventArgs e) {
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadFileAsync(new Uri("http://---/"), @"c:\");

private void Completed(object sender, AsyncCompletedEventArgs e) {
MessageBox.Show("Download completed!");

It seems that the download is working, but when I check the downloaded file I find it as 0 KB.

Any idea what's going on?


This works:

WebClient webClient = new WebClient();
webClient.Headers.Add("Accept: text/html, application/xhtml+xml, */*");
webClient.Headers.Add("User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
 webClient.DownloadFileAsync(new Uri(""),"");