本文介绍了如何在 .NET 中使用 WebBrowser 或 WebClient 检索 PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 .NET 自动执行 Web 文件的日常检索.

I am trying to automate the daily retrieving of a web file using .NET.

该文件是位于类似于以下地址的 PDF:

The file is a PDF located at an address similar to:

http://www.example.com/?s=doc20101022

这些是使用IE进行调试注册的HTTP请求的标头

and these are the headers of HTTP request registered for debug using IE

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) 组件获取并在本地保存它的方法?

Can you please suggest me a way to get it and save it locally using WebClient, WebBrowser or other VB.NET (Framework 4.0) components?

推荐答案

使用 DownloadFileDownloadFileAsync 方法 WebClient:

Use the DownloadFile or DownloadFileAsync method of 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 中需要它,它应该很容易移植.

You tagged the question with c# and only mentioned .NET in the subject so I've provided you with a C# solution. If you need it in VB.NET it should be easy to port though.

这篇关于如何在 .NET 中使用 WebBrowser 或 WebClient 检索 PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 06:10