【问题标题】:Download .pdf file from website. File is corrupt从网站下载 .pdf 文件。文件已损坏
【发布时间】:2014-10-09 13:56:02
【问题描述】:

我要下载以下pdf文件(点击后几秒内生成文件):

PDF Link

我尝试了以下代码:

static void DownloadByWebClient()
{
    string url = "http://www.sigmaaldrich.com/MSDS/MSDS/DisplayMSDSPage.do?country=NL&language=EN-generic&productNumber=271004&brand=SIAL&PageToGoToURL=null";
    string clientfile = @"C:\Users\Test\Downloads\newfile.pdf";

    WebClient wc = new WebClient();
    wc.UseDefaultCredentials = true;
    wc.Credentials = CredentialCache.DefaultCredentials;

    wc.DownloadFileAsync(new Uri(url, UriKind.Absolute), clientfile);
}

pdf 文件已创建。但是,当我尝试打开它时,我收到一条消息,它已损坏。 可能问题是文件在下载之前首先生成?

我也尝试过DownloadFile 方法。但随后抛出错误:

A first chance exception of type 'System.Net.WebException' occurred in System.dll
System.Net.WebException: An exception occurred during a WebClient request.
System.Configuration.ConfigurationErrorsException: 
Error creating the Web Proxy specified in the  'system.net/defaultProxy' configuration section.
---> System.Net.Sockets.SocketException: An invalid argument was supplied
at System.Net.SafeCloseSocketAndEvent.CreateWSASocketWithEvent(AddressFamily addressFamily,
SocketType socketType, ProtocolType protocolType, Boolean autoReset, Boolean signaled)
at System.Net.NetworkAddressChangePolled..ctor()
at System.Net.AutoWebProxyScriptEngine.AutoDetector.Initialize()
at System.Net.AutoWebProxyScriptEngine.AutoDetector.get_CurrentAutoDetector()
at System.Net.AutoWebProxyScriptEngine..ctor(WebProxy proxy, Boolean useRegistry)
at System.Net.WebProxy.UnsafeUpdateFromRegistry()
at System.Net.WebProxy..ctor(Boolean enableAutoproxy)
at System.Net.Configuration.DefaultProxySectionInternal..ctor(DefaultProxySection section)
at System.Net.Configuration.DefaultProxySectionInternal.GetSection()
--- End of inner exception stack trace ---

at System.Net.Configuration.DefaultProxySectionInternal.GetSection()
at System.Net.WebRequest.get_InternalDefaultWebProxy()
at System.Net.HttpWebRequest..ctor(Uri uri, ServicePoint servicePoint)
at System.Net.HttpRequestCreator.Create(Uri Uri)
at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
at System.Net.WebRequest.Create(Uri requestUri)
at System.Net.WebClient.GetWebRequest(Uri address)
at System.Net.WebClient.DownloadFile(Uri address, String fileName)
--- End of inner exception stack trace ---
at System.Net.WebClient.DownloadFile(Uri address, String fileName)
--- End of inner exception stack trace ---

可能是什么原因?

提前致谢!

【问题讨论】:

  • 你认为 DownloadFile Async 有什么作用?
  • 调用异步方法并立即处理? Please read documentation 特别是第一部分的备注。

标签: c# pdf webclient


【解决方案1】:

我已根据 OPs cmets 和我的测试更改了我的答案。 我可以运行以下代码,它工作得很好。文件下载好了,我本地磁盘上的pdf就不错了。

public void DLTest()
{
    string url = "https://www.osha.gov/Publications/Abate/abate.pdf";
    string clientfile = @"C:\Test\newfile3.pdf";

    WebClient wc = new WebClient();

    wc.DownloadFile(new Uri(url, UriKind.Absolute), clientfile);
}

但是,当我使用您的网址“http://www.sigmaaldrich.com/MSDS/MSDS/DisplayMSDSPage.do?country=NL&language=EN-generic&productNumber=271004&brand=SIAL&PageToGoToURL=null”时,pdf 不包含数据。您使用的网址似乎不支持以 PDF 格式下载此信息。

您可以尝试从其他站点下载 MSDS,例如下面的代码使用不同的 url。

public void DLTest()
{
    string url = "http://www.sciencelab.com/msds.php?msdsId=9927335";
    string clientfile = @"C:\Test\newfile.pdf";

    WebClient wc = new WebClient();

    wc.DownloadFile(new Uri(url, UriKind.Absolute), clientfile);
}

【讨论】:

  • @Bill W:我已经尝试过了,但随后抛出异常。
  • 有什么异常?
  • 我尝试使用 DownloadFile() 运行,我没有收到异常,但下载的文件已损坏;我会进一步调查。
  • 我认为问题在于pdf文件是根据文章编号生成的,并在完成时显示。如果单击该链接,您将看到生成 pdf 需要几秒钟。有没有办法首先触发pdf的生成,并在一段时间(例如30秒)后下载文件?
  • 您的网址未链接到 pdf 文件。它告诉他们的服务器获取信息并显示它,但您没有指向 pdf 的直接链接。我不知道你怎么能解决这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-19
  • 1970-01-01
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-09
相关资源
最近更新 更多