C#-重写WebBrowserDocumentCompletedEventHandler的事件参数

本文关键字:事件 参数 WebBrowserDocumentCompletedEventHandler 重写 C#- | 更新日期: 2023-09-27 18:20:21

我想覆盖事件WebBrowserDocumentCompletedEventArgs。我无法创建个人事件处理程序,因为我不知道什么时候应该触发事件DocumentDownloadCompleted。我想添加到EventArgs的数据是OriginalPageLink

我试图下载一个页面,但我被重定向到一个登录页面(只有一次)。我已经设置了一种登录方式,但我想重新尝试导航到原始页面,但我已经没有了。我可以设置一个全局变量来跟踪每次链接,但是,有办法编辑EventArgs吗?我是否也需要修改WebBrowserDocumentCompletedEventHandler

我的代码看起来像

private void Submit_Click(object sender, EventArgs e)
{
    webBrowser1 = new WebBrowser();
    webBrowser1.AllowNavigation = true;           
    webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
    webBrowser1.Navigate(OriginalPageLink);
}
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (e.Url.ToString().Contains("login.smlogin.ibb.ubs.net")) {
        loginWithWEBSSO(webBrowser1);
        webBrowser1.Navigate(***e.OriginalPageLink***);
    } else { 
        string mybody = webBrowser1.Document.Body.InnerText;
    }
}

提前感谢您的任何建议。

Marco

C#-重写WebBrowserDocumentCompletedEventHandler的事件参数

您只需要锁定WebBrowser.Navigating事件,就可以使用某种集合跟踪导航历史。返回登录前的上一页。Navigate事件将触发Navigation并删除以前的URL,因此您需要管理集合,或者只限制在您的状态下管理哪些项。

您可以为此使用一个私有字符串,但我认为您必须重新处理它。到目前为止,我看到了这个序列:

1. Navigate to Original page.
2. Presented with login page.
3. Fill in the username / password and then submit.
4. Navigate to Original page.
5. Step 3's process fires a DocumentCompleted event - URL still "login" (loop).
6. Step 4's process fires a DocumentCompleted.

这些步骤的时间并不保证,因此步骤4可能会提示您再次登录。

我总是先转到登录页面并登录。一旦触发DocumentCompleted,我就会转到原始页面。我使用私有变量跟踪所有这些。最简单的方法是"private int _step=1;"然后根据您所处的步骤来决定在DocumentCompleted中做什么。