401 web exception while downloading file

1.2k views Asked by At

I want to download a certain file from a website. For that, I tried using this

WebProxy p = new WebProxy("192.168.10.254:8080", true);
p.Credentials = new NetworkCredential("username", "pwd");
WebRequest.DefaultWebProxy = p;
WebClient client = new WebClient();
string downloadString = client.DownloadString("myDowloadUrl");

Each time, I get this web exception

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

Any clues?

1

There are 1 answers

2
Rahul On BEST ANSWER

I'd recommend to keep it simple always. Try this

       using (var Client = new WebClient())
        {
            Client.Credentials = new System.Net.NetworkCredential("username", "pwd");
            Client.DownloadFile("myDowloadUrl", @"downloadFilelocalpath");
        }