WebBrowser控制会话中的下载文件

g.f*_*ley 6 .net c# webclient webbrowser-control

我正在WebBrowser control浏览登录页面并下载文件.由于我无法使用控件自动管理下载,因此我正在使用WebClient该类来尝试实现此目的.

问题是,由于WebClient浏览器不在同一个上下文/会话中,我正在下载的是安全错误屏幕.

我如何可以传递的范围内任何想法WebBrowser会话到WebClient

小智 11

经过一周的谷歌搜索尝试解决方案后,我发现了一个非常简单!

我是你在HTTPS URL中静默下载文件的方法,而webbrowser控件就是这样做的.

1)使用webbrowser登录2)使用此代码下载.

//build de URL 

  string _url = "https://........."

  //define a download file name and location

  string _filename = @"C:\Users\John\Documents\somefile.pdf";

  //create a webcliente

  WebClient cliente = new WebClient();

  //do some magic here (pass the webbrowser cokies to the webclient)

  cliente.Headers.Add(HttpRequestHeader.Cookie, webBrowser1.Document.Cookie);

  //and just download the file

  cliente.DownloadFile(_urlpdf, _filename);
Run Code Online (Sandbox Code Playgroud)

它解决了我的问题


Lar*_*mie 4

这应该只是模拟 WebBrowser 会话中的 cookie 和标头并重新使用它们来模拟 WebClient 中的会话,但看起来您已经在这条路上很热衷了。

我将按以下步骤进行。

  1. 从 WebBrowser 获取 cookie 和标头。

    Cookie:您可以通过处理 WebBrowser 控件的 DocumentCompleted 事件并解析 DocumentCompleted 事件中设置的 cookie,从 WebBrowser 会话中获取 cookie。

    标头:使用像 Fiddler [www.fiddler2.com/] 这样的代理来读取标头,这样您就会知道服务器需要什么。

  2. 将上面收集​​的身份用于 WebClient。

    标头:迭代所有收集的标头,并确保将它们添加到 Web 客户端,myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");例如使用

    饼干:请参阅这篇文章