代码之家  ›  专栏  ›  技术社区  ›  a1oleg

webClient.DownloadFile()返回404

  •  0
  • a1oleg  · 技术社区  · 6 年前

     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");
            }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Ehsan Mohammadi    6 年前

    Web浏览器完成的请求和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");
    }
    

    希望对你有帮助