C# - Override eventargs of WebBrowserDocumentCompletedEventHandler

1.1k Views Asked by At

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

2

There are 2 best solutions below

0
On

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.

0
On

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.