【问题标题】:webClient.DownloadFile() return 404webClient.DownloadFile() 返回 404
【发布时间】:2019-01-26 19:50:09
【问题描述】:

浏览器用这个url下载文件没问题,但是webClient返回404

 string url = "http://zakupki.gov.ru/44fz/filestore/public/1.0/download/priz/file.html?uid=19CC93BEA67C4650B51D69CAA28CB27D";      
 using (var webClient = new WebClient())
        {                          
            webClient.DownloadFile(url , "name");
        }

【问题讨论】:

  • 可能需要设置UserAgent。
  • C# Webclient returning error 404 可能会对您有所帮助。如果可以,请查看回复。
  • 这是一个用俄语编写的 Word 文档 (.docx)。它与你有关吗?因为现在是公开的。 User-Agent 标头建议是正确的。

标签: c# asp.net http-status-code-404 webclient


【解决方案1】:

Web Browser 发出的请求和WebClient 发出的请求是有区别的。

您需要将此添加到您的代码中:

webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

所以你的代码会改成这样:

string url = "http://zakupki.gov.ru/44fz/filestore/public/1.0/download/priz/file.html?uid=19CC93BEA67C4650B51D69CAA28CB27D";
using (var webClient = new WebClient())
{
  webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
  webClient.DownloadFile(url, "name.docx");
}

希望对你有帮助

【讨论】:

  • 一个小建议:与其对字符串进行硬编码,不如使用实际值在语义上更正确(尽管最终并不重要),即$"Mozilla/4.0 (compatible; MSIE 6.0; {Environment.OSVersion.VersionString}; .NET CLR {Environment.Version};)"
猜你喜欢
  • 2013-06-09
  • 1970-01-01
  • 1970-01-01
  • 2016-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-12
相关资源
最近更新 更多