【问题标题】:WebBrowser Control download file in sessionWebBrowser 控制会话中的下载文件
【发布时间】:2010-11-20 08:13:49
【问题描述】:

我正在使用WebBrowser control 浏览登录页面并下载文件。由于我找不到使用控件自动管理下载的方法,我正在使用WebClient 类来尝试实现这一目标。

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

关于如何将WebBrowser 会话的上下文传递给WebClient 的任何想法?

【问题讨论】:

标签: c# .net webbrowser-control webclient


【解决方案1】:

在谷歌搜索一周后,我找到了一个非常简单的解决方案!

我知道如何在 HTTPS URL 和网络浏览器控件中静默下载文件。

1) 使用网络浏览器登录 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);

解决了我的问题

【讨论】:

  • 谢谢!我确实工作过。你为我节省了很多时间。
  • 你用饼干保护了我的生命!!
  • 我正在寻找的答案!非常感谢!
【解决方案2】:

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

这就是我将如何继续。

  1. 从 WebBrowser 获取 cookie 和标头。

    Cookies:您可以通过处理 WebBrowser 控件的 DocumentCompleted 事件并从 DocumentCompleted 事件解析 cookie 集来从 WebBrowser 会话中获取 cookie。

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

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

    标头:遍历所有收集的标头,并确保它们是added to the webclient,例如使用myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");

    Cookie:见this post

【讨论】:

  • 我开始测试这个。这需要一些时间,但我会让你知道我是怎么走的。谢谢
猜你喜欢
  • 2013-12-11
  • 2012-04-21
  • 2013-03-03
  • 1970-01-01
  • 2014-09-17
  • 2011-08-04
  • 1970-01-01
  • 2023-03-21
  • 2013-11-26
相关资源
最近更新 更多