Click here to Skip to main content
15,917,481 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
I have been given a task to download a file from URL for which i have written the following function

void DownloadFile(string downloadLocation, string destinationLocation)
   WebClient myWebClient = new WebClient();
   myWebClient.DownloadFile(downloadLocation, destinationLocation);

The above code seems to be working fine on a number of files I ve tried to download but cannot work at all on one particular domain. What could be the issues?? and ofcourse the solution?

for Eg:

DownloadFile("", @"C:\1.jpg"); //works fine
DownloadFile("", @"C:\2.jpg"); //works fine
DownloadFile("", @"C:\21.jpg"); //works fine


DownloadFile("", @"C:\a.jpg"); //doesn't work
DownloadFile("", @"C:\abcd.jpg"); //doesn't work 

for those who think i am joking around ... all the files are easily downloadable when called via any browser including IE, firefox and chrome
Updated 15-Jun-13 12:21pm
Sergey Alexandrovich Kryukov 14-Jun-13 20:22pm    
Can you download it using some existing software, such as a Web browser?
Hasham Ahmad 15-Jun-13 18:18pm    
yeah ofcourse ... thats why i am worried sergey ...

1 solution

One possibility, and I can't really remember if I've encountered it before, is sometimes certain proxies and whatnot on the server may prohibit people from downloading their files. Each server has a different configuration, and maybe this one is blocking certain web requests. May I ask, what is the actual domain? (I take it it's not
Share this answer
Hasham Ahmad 19-Jun-13 1:57am    
The server configuration is indeed blocking my webclient.downloadfile() function but the questions still remains that how the browser manages to download the file when the URL is called directly, can we call a function which can create a request similar to that of a web browser?
RadXPictures 19-Jun-13 11:30am    
It may have to do with HTML header files (just a thought). Unfortunately I don't have much experience there, so you would have to investigate that. All I know is you can add headers via WebClient.Headers.Add(). You could also try putting your code in a Try/Catch block:

//your code
Catch (Exception ex)
RadXPictures 19-Jun-13 11:31am    
^ it would at least give you a more specific error description
Hasham Ahmad 20-Jun-13 4:09am    
The error is unable to connect to the server
anywayz ... i have used the different technique for the task but still wonder why the request generated by webclient.downloadfile is different from a normal web browser request

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900