2014-10-09 137 views
1

我想下载下面的PDF文件(该文件点击它后几秒钟内产生的):从网站下载.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 --- 

可能是什么原因?

在此先感谢!

+2

你认为什么是DownloadFile * Async *呢? – 2014-10-09 13:59:21

+3

调用异步方法并立即处理? [请阅读文档](http://msdn.microsoft.com/en-us/library/ms144196(v = vs.110).aspx)特别说明第一部分。 – Reniuz 2014-10-09 14:00:56

回答

0

我根据OP的评论和我的测试改变了我的答案。 我可以运行下面的代码,它工作得很好。该文件被下载,并在我的本地磁盘上的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); 
} 
+0

@比尔W:我已经尝试过,但后来抛出异常。 – 2014-10-09 14:25:27

+0

什么是例外? – 2014-10-09 14:31:24

+0

我试着用DownloadFile()运行,我没有得到异常,但下载的文件已损坏;我会进一步研究。 – 2014-10-09 14:51:22