C# – WebClient generates (401) Unauthorized error

cdownloadwebclient

I have the following code running in a windows service:

WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential("me", "12345", "evilcorp.com");
webClient.DownloadFile(downloadUrl, filePath);

Each time, I get the following exception

{"The remote server returned an error: (401) Unauthorized."}

With the following inner exception:

{"The function requested is not supported"}

I know for sure the credentials are valid, in fact, if I go to downloadUrl in my web browser and put in my credentials as evilcorp.com\me with password 12345, it downloads fine.

What is weird though is that if I specify my credentials as me@evilcorp.com with 12345, it appears to fail.

Is there a way to format credentials?

Best Answer

webClient.UseDefaultCredentials = true; resolved my issue.