Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 401尝试下载图像时发生未经授权的错误_C#_.net_Webclient - Fatal编程技术网

C# 401尝试下载图像时发生未经授权的错误

C# 401尝试下载图像时发生未经授权的错误,c#,.net,webclient,C#,.net,Webclient,我正在尝试使用以下代码从URL下载图像: using (System.Net.WebClient webClient = new System.Net.WebClient()) { NetworkCredential Cred = new NetworkCredential(); Cred.UserName = "user"; Cred.Password = "password^"; webClient.Credentials = Cred; image

我正在尝试使用以下代码从URL下载图像:

using (System.Net.WebClient webClient = new System.Net.WebClient())
{
    NetworkCredential Cred = new NetworkCredential();
    Cred.UserName = "user";
    Cred.Password = "password^";

    webClient.Credentials = Cred;
    imageBytes = webClient.DownloadData(s);
}
但是,我得到了一个错误:远程服务器返回了一个错误:401 Unauthorized。 内部没有例外。我给我的crendentials,我可以下载图像时,我把他们直接粘贴到IE。
我只是无法找出造成这种情况的原因。

设置webClient.UseDefaultCredentials=true;解决了我的问题

见哦,老兄!非常感谢。。这件事我已经做了很长时间了。