下载文件时出现401 Web异常

时间:2014-01-01 05:54:16

标签: c# webclient webclient-download defaultproxy

我想从网站下载某个文件。为此,我尝试使用这个

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

每一次,我都会遇到此网络异常

  

远程服务器返回错误:(401)未经授权。

任何线索?

1 个答案:

答案 0 :(得分:2)

我建议一直保持简单。试试这个

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