我正在尝试使用.NET自动化每日检索Web文件。
该文件是PDF,位于类似于:
的地址http://www.example.com/?s=doc20101022
这些是使用IE
注册调试的HTTP请求的标头HTTP/1.1 200 OK
Server: Apache/2.2.3 (CentOS)
Vary: User-Agent,Accept-Encoding
Expires: 0
Cache-Control: must-revalidate, post-check=0, pre-check=0
Pragma: public
Last-Modified: Mon, 22 Nov 2010 22:45:12 GMT
Cache-Control: private
Content-Disposition: attachment; filename="doc20101022.pdf"
Content-Transfer-Encoding: binary
Content-Type: application/force-download
Date: Tue, 23 Nov 2010 10:41:43 GMT
X-Varnish: 2155914052
Via: 1.1 varnish
Content-Length: 6596997
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Age: 2
您能否建议我使用WebClient,WebBrowser或其他VB.NET(Framework 4.0)组件在本地保存并保存它?
答案 0 :(得分:2)
使用DownloadFile的DownloadFileAsync或WebClient方法:
WebClient wc = new WebClient();
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(delegate(object source, AsyncCompletedEventArgs args) {
// Do something when the file has been downloaded successfully.
});
wc.DownloadFileAsync(new Uri("http://www.example.com/?s=doc20101022"), @"C:\Yourfile.pdf");
编辑:您使用c#标记了问题,并在主题中仅提到了.NET,因此我为您提供了C#解决方案。如果你在VB.NET中需要它,它应该很容易移植。