4

我有一些代码会引发 407 未经授权的异常。

我正在尝试下载一个文件,下面是我的示例代码。我尝试过使用 netcredentials 和 webproxy,但徒劳无功。

WebClient webClient = new WebClient();
NetworkCredential netCred=new NetworkCredential();
netCred.UserName="<<userid>>";
netCred.Password="<<password>>";
netCred.Domain="<<windowsdomainname>>";
webClient.Credentials = netCred;
WebProxy wp = new WebProxy();
wp.Credentials = netCred;
wp.Address = new Uri(@"http://proxy-xx.xxxx.co.uk:8080/proxy.pac");
webClient.Proxy = wp;
webClient.DownloadFile("http://www.win-rar.com/postdownload.html?&L=0", @"c:\winrar.exe");
4

2 回答 2

0

在找到您的问题之前,我得到了 407。将您的源代码修改为以下内容,这对我有用:

try
{
    var netCred = new NetworkCredential { UserName = "ununun", Password = @"pwpwpw", Domain = @"domain" };
    var webProxy = new WebProxy { Credentials = netCred };
    var webClient = new WebClient { Proxy = webProxy };
    webClient.DownloadFile(url, saveFileName);
}
catch (Exception ex)
{
    Console.WriteLine("Exception:\n{0}\n{1}", ex.Message, ex.StackTrace);
    return;
}
于 2014-01-28T21:27:14.323 回答
0

This is what I do yesterday.

WebClient client = new WebClient();
ICredentials cred;
cred = new NetworkCredential(user, password);
client.Proxy = new WebProxy("xxxx-proxy", true, null, cred);

Hope it help !

于 2016-03-02T01:43:16.833 回答