【问题标题】:401 unauthorized error while trying to download an image [duplicate]尝试下载图像时出现 401 未经授权的错误 [重复]
【发布时间】:2015-09-24 23:22:48
【问题描述】:

我正在尝试使用以下代码从 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。 没有内在的例外。我正在提供我的凭据,当我将它们直接粘贴到 IE 时,我可以下载图像。 我只是无法弄清楚是什么原因造成的。

【问题讨论】:

标签: c# .net webclient


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-15
    • 2014-06-25
    • 1970-01-01
    • 2021-05-28
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    相关资源
    最近更新 更多