C# - 覆盖 WebBrowserDocumentCompletedEventHandler 的 eventargs

发布于 12-27 01:48 字数 1105 浏览 5 评论 0原文

我想重写事件 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;
    }
}

提前感谢您的任何建议。

马可

I would like to override the EventArgs of the event WebBrowserDocumentCompleted. I can't create a personal event handler, because I have no idea when I should fire the event DocumentDownloadCompleted. The data I would like to add to the EventArgs is the OriginalPageLink.

I'm trying to download a page, but I'm redirected to a login page (only once). I've setup a way to login, but then I would like to re-try navigating to the original page, but I don't have it any more. I could set a global variable to track each time that link, but, is there a way to edit EventArgs? Do I need to also modify WebBrowserDocumentCompletedEventHandler.

My code looks like

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;
    }
}

Thank you in advance for any suggestions.

Marco

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最美的太阳2025-01-03 01:48:44

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

You just need to latch onto the WebBrowser.Navigating event to keep track of the navigation history using some sort of collection. Go back to the previous page before the login occurs. The Navigate event will trigger the Navigating and remove the previous URL, so you need to manage a collection or just limit which items get managed in your state.

妥活2025-01-03 01:48:44

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

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 中执行哪些操作。

You can use a private string for this, but I'm thinking you have to rework this. So far I see this sequence:

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.

The timing of the steps is not guaranteed so step 4 might prompt you for a login again.

I worked around this by always going to the login page first and logging in. Once that fires a DocumentCompleted I would then go to the original page. I kept track of all of this using private variables. The simplest would be "private int _step=1;" Then deciding what to do in DocumentCompleted based on which step you were on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文