我想下载下面的PDF文件(该文件点击它后几秒钟内产生的):从网站下载.pdf文件。文件损坏
我尝试下面的代码来做到这一点:
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 *呢? – 2014-10-09 13:59:21
调用异步方法并立即处理? [请阅读文档](http://msdn.microsoft.com/en-us/library/ms144196(v = vs.110).aspx)特别说明第一部分。 – Reniuz 2014-10-09 14:00:56