简体   繁体   中英

"An exception occurred during a WebClient request" when using the Task Scheduler

I have a console app that downloads certain files from an FTP server. I'm using WebClient to do this like so:

var cred = new NetworkCredential(user, password);
var downloadRequest = new WebClient();
downloadRequest.Credentials = cred;
downloadRequest.DownloadFile(host + FileName, target + FileName);

This works perfectly when I run the app normally. However, I need to run this in the weekend so I have had Task Scheduler run it on Sunday. But when I do, I always get this error in my log:

An exception occurred during a WebClient request

When I rerun it manually, it works perfectly. Is there anything I'm overlooking?

This could help others looking for solution.

I believe this line:

downloadRequest.DownloadFile(host + FileName, target + FileName);

is doing the trick. Somehow it just doesn't recognize the FileName (second parameter).

What worked for me is, I changed the above line to the following:

downloadRequest.DownloadFileAsync(new Uri(host + FileName), target + FileName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM