1

I am using WebBrowser to login certain website. Login itself is okay, but it opens new window and I'm losing control of it. Below is code snippet.

            HtmlDocument htmlDocAceCounter = this.webBrowser1.Document;

            HtmlElement elementLoginID = htmlDocAceCounter.GetElementById("userid");
            HtmlElement elementPassword = htmlDocAceCounter.GetElementById("userpasswd");

            if ((elementLoginID != null) && (elementPassword != null))
            {
                elementLoginID.SetAttribute("Value", "someid");
                elementPassword.SetAttribute("Value", "somepw");
                webBrowser1.Navigate("www.loginURL.com");
            }

Problem is that it opens new windows with encrypted web address first and reaches main webpage later. So I can't get the control of logged in page, as well as I don't know how to get there.

Is there anyway to workaround this issue? Any advice would be appreciated.

Edit: It's not duplicate exactly. It's my bad explanation. It first opens new window with loginURL.com, goes through encrypted web address, and then opens logged in page. I don't know how it detects it, but it only approves valid access from direct click popup. I tried many things, but I couldn't cut in the middle. With the referred example, it doesn't reach the encrypted web address and just stays at loginURL.com. I hope it explains better.

Edit2: From

private void Web_V1_NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData,string Headers, ref bool Processed)
{
Processed = true; //Stop event from being processed

//Code to open in same window
this.webBrowser1.Navigate(URL);

//Code to open in new window instead of same window
//Form1 Popup = new Form1();
//Popup.webBrowser1.Navigate(URL);
//Popup.Show();
}

Header has Content-Type: application/x-www-form-urlencoded and PostData has something that can be translated into ascii character. How do I translate them into right url address?

Edit3: I used

this.webBrowser1.Navigate(URL, TargetFrameName, (byte[])PostData, Headers);

instead of

this.webBrowser1.Navigate(URL);

and now I'm getting COMException (0x80004005): Error HRESULT E_FAIL. Does anyone have any idea?

Brian Cho
  • 37
  • 11
  • See here too: http://stackoverflow.com/questions/175836/system-windows-forms-webbrowser-open-links-in-same-window-or-new-window-with-same – spender Jun 18 '15 at 10:23

1 Answers1

0

Okay. I found an answer. COMException (0x80004005): Error HRESULT E_FAIL was occurred because I tried to access main thread object from COM thread. Below is what I did.

public partial class Form1 : Form
{
    WebBrowser browser;
    SHDocVw.WebBrowser_V1 browserV1AceCounter;

    byte[] bPostData;
    string sHeader;
    string sURL;

    public Form1()
    {
        InitializeComponent();

        browser = new WebBrowser();

        browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(browserLoaded);
        browser.NewWindow += new CancelEventHandler(WebBrowserNewWindow);
    }

    private void WebBrowserNewWindow(Object sender, CancelEventArgs e)
    {
        browserV1AceCounter = (SHDocVw.WebBrowser_V1)browser.ActiveXInstance;
        browserV1AceCounter.NewWindow += Web_V1_NewWindow;
    }

    private void Web_V1_NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
    {
        try
        {
            Processed = true; //Stop event from being processed

            // Copy necessary info to use in main thread
            sURL = URL;
            bPostData = (byte[])PostData;
            sHeader = Headers;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    private void browserLoaded(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        try
        {
            if (e.Url.ToString().Contains("loginURL.com"))
            {
                this.browser.Navigate(sURL, null, bPostData, sHeader);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

You need to make sure the sequence is alright and fill in the rest part but, in case of you need to go through popup window with encrypted post data, above code snippet should work.

Brian Cho
  • 37
  • 11