C# - 覆盖 WebBrowserDocumentCompletedEventHandler 的 eventargs
我想重写事件 WebBrowserDocumentCompleted
的 EventArgs
。我无法创建个人事件处理程序,因为我不知道何时应该触发事件 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 技术交流群。

发布评论
评论(2)
您可以为此使用私有字符串,但我认为您必须重新处理它。到目前为止,我看到的是这样的顺序:
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 中执行哪些操作。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您只需锁定
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. TheNavigate
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.