使用WebBrowser自动下载,不含网址 [英] Automated filedownload using WebBrowser without url
问题描述
我一直在使用System.Windows.Forms.WebBrowser在C#编写的WebCrawler。我正在尝试从一个网站下载一个文件并将其保存在本地机器上。更重要的是,我想这样做是完全自动化的。可以通过单击一个按钮来启动文件下载,该按钮将调用一个javascript功能,激发下载显示您要打开还是保存此文件?对话框。我绝对不想手动点击另存为,并输入文件名。
I've been working on a WebCrawler written in C# using System.Windows.Forms.WebBrowser. I am trying to download a file off a website and save it on a local machine. More importantly, I would like this to be fully automated. The file download can be started by clicking a button that calls a javascript function that sparks the download displaying a "Do you want to open or save this file?" dialog. I definitely do not want to be manually clicking "Save as", and typing in the file name.
我知道HttpWebRequest和WebClient的下载功能,但是由于下载是以javascript开头的,所以我现在知道文件的URL。 Fyi,javascript是一个doPostBack函数,它改变一些值并提交一个表单。
I am aware of HttpWebRequest and WebClient’s download functions, but since the download is started with a javascript, I do now know the URL of the file. Fyi, the javascript is a doPostBack function that changes some values and submits a form.
我已经尝试着重从WebBrowser保存为对话框,使其在内部自动化,而没有太多的成功。我知道有一种方法来强制下载保存,而不是通过向http请求添加头来保存或打开,但是我不知道如何指定要下载的文件路径。
I’ve tried getting focus on the save as dialog from WebBrowser to automate it from in there without much success. I know there’s a way to force the download to save instead of asking to save or open by adding a header to the http request, but I don’t know how to specify the filepath to download to.
推荐答案
我认为您应该防止下载对话框甚至显示。这可能是一种方法:
I think you should prevent the download dialog from even showing. Here might be a way to do that:
-
Javascript代码导致您的WebBrowser控件导航到特定的Url导致下载对话框出现)
The Javascript code causes your WebBrowser control to navigate to a specific Url (what would cause the download dialog to appear)
为了防止WebBrowser控件实际浏览到此Url,请将事件处理程序附加到导航事件。
To prevent the WebBrowser control from actually Navigating to this Url, attach a event handler to the Navigating event.
在您的导航事件中,您必须分析这是否是您想要停止的实际导航操作(这是下载网址,也许检查文件扩展名,必须有可识别的格式)。使用WebBrowserNavigatingEventArgs.Url来执行此操作。
In your Navigating event you'd have to analyze if this is the actual Navigation action you'd want to stop (is this one the download url, perhaps check for a file extension, there must be a recognizable format). Use the WebBrowserNavigatingEventArgs.Url to do so.
如果这是正确的URL,请通过设置WebBrowserNavigatingEventArgs.Cancel属性停止导航。
If this is the right Url, stop the Navigation by setting the WebBrowserNavigatingEventArgs.Cancel property.
使用HttpWebRequest或WebClient类继续下载
Continue the download yourself with the HttpWebRequest or WebClient classes
有关该事件的更多信息,请查看此页面:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.navigating.aspx
Have a look at this page for more info on the event:
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.navigating.aspx
这篇关于使用WebBrowser自动下载,不含网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!