我有一些代码会抛出 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");
最佳答案
在找到您的问题之前,我遇到了 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;
}
关于c# - 如何将代理身份验证详细信息添加到 webclient 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11228744/