2014-10-09 134 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