Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'An exception occurred during a WebClient request" while using C# ASP.NET

Tags:

c#

So, I have built an auto update program to my program.

The code that is running in here is:

new WebClient().DownloadFile("XXXX", checkingfolder.SelectedPath); 

the XXX is my webserver that is running as a VPS server in verio, with the newest IIS and everything.

When the user clicks on the download button, it says:

'An exception occurred during a WebClient request. 

The thing is, that I dont even know why - i am just doing try catch.

Anyone here have any idea why this happened?

Thanks for any help you will give me, you have no idea how much you are helping me here - thanks again !

like image 580
user1032254 Avatar asked Nov 07 '11 07:11

user1032254


People also ask

What does an exception occurred during a WebClient request mean?

If Directory does not exist,this error message comes as 'An exception occurred during a WebClient request" Because Web Client Does not find the Folder to store downloaded files.

What is a WebClient request?

The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI. The WebClient class uses the WebRequest class to provide access to resources.


2 Answers

I can reproduce this if I specify, as seems to be the case in your example, a folder name rather than a file name the destination. Supply a file name instead.

As an aside; if I look at the InnerException, it tells me that the problem relates to the file path:

using(var client = new WebClient()) {     try     {         client.DownloadFile(             "http://stackoverflow.com/questions/8033619/an-exception-occurred-durning-a-webclient-request-c-sharp-asp-net/8033687#8033687",             @"j:\MyPath");     }     catch (Exception ex)     {         while (ex != null)         {             Console.WriteLine(ex.Message);             ex = ex.InnerException;         }     } } 

Which gives:

An exception occurred during a WebClient request. Access to the path 'j:\MyPath' is denied. 

If I change it to a file, it works fine:

client.DownloadFile(     "http://stackoverflow.com/questions/8033619/an-exception-occurred-durning-a-webclient-request-c-sharp-asp-net/8033687#8033687",     @"j:\MyPath\a.html"); 
like image 186
Marc Gravell Avatar answered Oct 08 '22 13:10

Marc Gravell


Sometimes this error can occur when another class or process is accessing the file you've just downloaded

like image 42
jbird Avatar answered Oct 08 '22 13:10

jbird