C# – The remote server returned an error: (401) Unauthorized Error occured when i try to download file via Web Client

authenticationc

I have the following code running in a windows form Application:

WebClient client = new WebClient();
WebProxy wp = new WebProxy("http://50.35.125.91:81/");
client.UseDefaultCredentials = true;
wp.Credentials = CredentialCache.DefaultCredentials;
wp.Credentials = new NetworkCredential("matif", "yyy", "xyz");


client.Proxy = wp;
client.DownloadFile("http://50.35.125.91:81/abc/AppUpdate.xml", @"D:\abc.xml");

Each time, I get the following exception

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

I know for sure the credentials are valid,

Best Answer

Provide credentials to proxy and client:

using (WebClient client = new WebClient())
{
    WebProxy wp = new WebProxy("http://50.35.125.91:81/");
    wp.Credentials = new NetworkCredential("matif", "yyy", "xyz");

    client.UseDefaultCredentials = false;
    client.Credentials = wp.Credentials;
    client.Proxy = wp;
    client.DownloadFile("http://50.35.125.91:81/abc/AppUpdate.xml", @"D:\abc.xml");
}
Related Topic