如何从Web下载文件而不会收到404错误 [英] How to download a file from web without getting 404 error

查看:115
本文介绍了如何从Web下载文件而不会收到404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从C#编写一个cosole应用程序,从网页下载一些pdf文件。我正在使用Web客户端来完成这项工作。问题是Web客户端返回错误404.但是当我通过下载链接复制到浏览器时,它正常工作。我怎么能克服这一点。



(尝试在方法中将ym996,k36062,k15547,j63390值作为'pdfId'发送)



我的尝试:



Im writing a cosole app from C# to download some pdf files from a web page. And im using web client to do this work. The problem is web client returns a error 404. But when i copy past the download link to browser it works properly. How can i overcome this.

(try sending ym996, k36062, k15547, j63390 values as 'pdfId' in the method)

What I have tried:

private bool DownloadFile(string pdfId, string filetype)
{
      try
      {
           using (WebClient webClient = new WebClient())
           {
                   webClient.DownloadFile("http://munifilings.com/pdfs/0/" + pdfId + ".pdf", "E:\\Backups\\File" + filetype + "_" + pdfId + ".pdf");
           }
           return true;
      }catch (Exception ex)
      {
                Execption = ex.Message;
                return false;
      }
}

推荐答案

404错误在技术上意味着找不到文件 - 所以首先检查您正在使用的确切路径,并尝试在干净的浏览器中准确下载该文件 - 即删除所有cookie,确保您没有登录该站点。如果它在浏览器中工作,那么它应该在您的应用程序中工作 - 但如果远程站点需要登录,如果授权失败,您也可能会收到相同的错误。所以然后像往常一样登录到站点并再试一次。

当我尝试访问包含pdf文件的文件夹时,我收到403(授权失败)错误,所以很可能是你的WebClient需要授权才能访问文件。

尝试类似:

A 404 error technically means "file not found" - so start by checking the exact path you are using, and try downloading exactly that file in a "clean" browser - i.e. delete all cookies, make sure you aren't logged in to the site. If it works in a browser, then it should work in your app - but if the remote site requires a login you could also get the same error if the authorisation fails. So then log in to the site as you would normally and try again.
When I try to access the folder containing the pdf files, I get a 403 (authorization failed) error, so it's likely that your WebClient needs authorisation to access the files.
Try something like:
using (WebClient webClient = new WebClient())
   {
   webClient.Credentials = new NetworkCredential("username", "password");


这篇关于如何从Web下载文件而不会收到404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆