【问题标题】:How to use WebClient.DownloadFile with digest authentication and a query string如何使用带有摘要式身份验证和查询字符串的 WebClient.DownloadFile
【发布时间】:2014-08-15 11:26:36
【问题描述】:

如何将WebClient.DownloadFile 与摘要式身份验证和查询字符串一起使用?

当我尝试使用它时,我得到了 401 响应。

这是 Apache 错误日志:

[Tue Jun 24 17:31:49 2014] [error] [client x.x.x.x] Digest: uri mismatch - </file-1.php> does not match request-uri </file-1.php?since=1403587422>

这是我尝试下载文件的方法:

Uri uri = new Uri("http://example.com/file-1.php?since=1403587422");
WebClient webClient = new WebClient();
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(
  new Uri(uri.GetLeftPart(UriPartial.Authority)),
  "Digest",
  new NetworkCredential("username", "password")
);
webClient.Credentials = credentialCache;
webClient.DownloadFile(uri, file.OutputFile);

【问题讨论】:

  • 您应该在请求后得到 401 响应。 401 响应包含计算质询响应所需的 WWW-Authenticate 标头。查看摘要身份验证协议的工作原理。 technet.microsoft.com/en-us/library/cc780170(v=ws.10).aspx
  • @MikeHixson 我得到了 401 罚款,之后的请求失败了。
  • 尝试以下链接中的解决方法connect.microsoft.com/VisualStudio/feedback/details/571052/…>
  • 仅出于调试目的对 credentialCache "http://example.com" 上的 url 进行硬编码,看看是否有任何变化。

标签: c# .net apache webclient digest-authentication


【解决方案1】:
WebClient webCl = new WebClient();
webCl.Credentials = new NetworkCredential("username", "Password");
webCl.DownloadFile(file download URL, fil save path with FileName);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多