27

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 !

4
  • 2
    Log the details of the exception. That should show what's going on.
    – Jon Skeet
    Commented Nov 7, 2011 at 7:08
  • I very much doubt that the details of the exception including "durning" a WebClient request. Are you sure you don't have code which is swallowing the WebClient exception and just logging that message without doing anything with the exception?
    – Jon Skeet
    Commented Nov 7, 2011 at 7:13
  • @Jon typo aside, I can repro something similar enough to be convincing Commented Nov 7, 2011 at 7:23
  • 3
    @MarcGravell: I suspect you're right - but by the details of the exception, I'd have expected the inner exception too. (That should always be checked...)
    – Jon Skeet
    Commented Nov 7, 2011 at 7:25

6 Answers 6

54

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");
0
4

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

3

it gives exception, if directory to path does not exist.

for example path is @"j:\Folder\SubFolder\123.pdf and SubFolder does not exist ,it will throw exception.

2

I ran into this error when I was trying to download a file, where the resulting downloaded file path would have been longer than some arbitrary limit

After changing the downloaded file's path name to be 250 characters long, the problem went away

1

Beside other answers, beware that the same WebException might also occur if the client process does not have needed permission to create output file.

I would suggest you to take the following strategy:

  1. Download file to a unique filename with .tmp (.txt) extensions Windows Temporary Folder to avoid write-permission and other permissions issues
  2. Move temporary file to destination folder
  3. Rename temporary file to destination filename

Hope it helps :-)

1

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.

Hope it Helps-:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.