WebBrowser certificates and pop-ups

mentalhard

Well-known member
Joined
Aug 7, 2006
Messages
123
Programming Experience
Beginner
I have embedded the WebBrowser control v.2.0 in Windows form and i've coded almost evertyhing that IE 6 supports (back, forward, refresh, options, properties, print ... ).

What i am struggling with is Support for the certificates and pop-ups.
When the dialog for certificates appears it refers to real Internet explorer and my Browser just hangs up. As for the pop-up windows i need that all popups be opened in a new tab in my browser. I have made the functions that creates tabpage i just need to catch that it's pop-up and call the function.

Thanks :)
 
Ok i've tried with newWindow event but it seems like i am always refering the current url instead the url of the popup.
VB.NET:
Private Sub browser_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        e.Cancel = True 'prevent to jump in new window
        Me.AddTabPageAndBrowser() 'add a new tabpage
        Me.tabHolder.TabIndex = Me.tabHolder.TabPages.Count - 1
        currentBrowser.Url = CType(sender, WebBrowser).Url
End Sub

but it always opens the same uri again and again.

Thanks :)
 
Hmmm it makes sense but i was really not able to find a way to process the URI to the new webBrowser instance.

Any further help would be greatly appreciated.

Thanks :)
 
Well actually i am using addhandler statement to add the events to the WebBrowser (which is created in runtime) so i have no idea which event occurs first. Thanks.
 
AddHandler statement doesn't have anything to do with the order which events occur. You can use code like this to debug events:
VB.NET:
Private Sub WebBrowser1_Navigating(ByVal sender As Object, _
ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
    Console.WriteLine("Navigating " & Date.Now.ToString("ss fffffff"))
End Sub

Private Sub WebBrowser1_NewWindow(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
    Console.WriteLine("NewWindow " & Date.Now.ToString("ss fffffff"))
End Sub
As you can see Navigating event doesn't occur in relation to NewWindow event at all.

From what I have found you can't get the information you want from the .Net 2.0 Webbrowser control, but a quick search revealed a workaround by Riquel Dongs post. To recite: add reference to COM library Microsoft Internet Controls, cast Webbrowser.ActiveXInstance to SHDocVw.WebBrowser, handle the NewWindow3 event of this object. Similarly:
VB.NET:
Private Sub frmDiv_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    WebBrowser1.GoHome()
    svd = DirectCast(Me.WebBrowser1.ActiveXInstance, SHDocVw.WebBrowser)
End Sub

Private WithEvents svd As SHDocVw.WebBrowser

Private Sub svd_NewWindow3(ByRef ppDisp As Object, ByRef Cancel As Boolean, ByVal dwFlags As UInteger, _
ByVal bstrUrlContext As String, ByVal bstrUrl As String) Handles svd.NewWindow3
    Cancel = True
    Me.Text = bstrUrl 'here's the url
End Sub
The Webbrowser control needs to navigate once to initialize ActiveXInstance before you can get the reference, it doesn't need to be a page, you can navigate to "about:blank" also first. If the control is visible it doesn't need navigating, so if Shown event could have been used.
 
I just tested it and it works like a charm. Thanks for the help John :)
If you don't mind i would ask for just one more try. Actually i did mention it in the oroginal post; how to make that installed and accepted certificates refer to the current WebBrowser instance insted to IE like it currently does. When i select the certificate and click OK nothing happens.
In IE and the same website with the same certificate works flawless.

Thanks :)
 
opps i think i celebrated too early.

it doesn't work properly for real popups only if you select "Open in a new window" from the context menu.

for example if you check http://www.geekpedia.com you will notice that when popup should appear you actually get an error :
Unterminated string constant

Thanks :)
 
I didn't get a popup from that url, but you can do it like this:
VB.NET:
Private Sub svd_NewWindow2(ByRef ppDisp As Object, ByRef Cancel As Boolean) Handles svd.NewWindow2
    Dim svd2 As SHDocVw.WebBrowser = WebBrowser2.ActiveXInstance
    svd2.RegisterAsBrowser = True
    ppDisp = svd2.Application
End Sub
WebBrowser2 is the browser control to receive the newwindow navigation, it have to be visible.
 
It makes sense but from some reason it doesn't work properly. Sometimes it is displayed in a new tabpage plus one IE window instance popups too.

I reproduced the problem making one very basic browser that is attached so please just check what i am doing wrong. It seems fine but still doesn't handle the popups very well. Also the webpages that requires certificates.

Please check that out. Very basic (just navigation and NewWindow2/3 events)

Thanks :)


EDIT: i was wondering why i cannot give you more reputations for your replies? It says: you must spread blah blah. Thanks!!!
 

Attachments

  • PopuUp.zip
    16.3 KB · Views: 25
Use either Newwindow2 or 3 and don't cancel it. If you need to check the url to filter out some newwindows use 2 (and then use cancel to stop it).

You could also benefit from using the Navigate method of browser instead of the Url property and handling endless URI possibilities.
VB.NET:
currentBrowser.Navigate(Me.txtURL.Text)
 
It makes sense to me .. thanks John i will try that right now.

By the way, did you consider the question about opening websites that require certificates. Usually what happens here is that when i select sucha website and i have needed certificate installed already the certificates window popups and i select an appropreate cert. but nothing happens. If i do the same through IE directly it works just fine. Means same website, same certificate and everthing is fine. WebBrowser control doesn't accept that. I am sure i need to take some actions in order to make it ...

Thanks :)
 
I just logged into a secure site with a certificate and it worked out of the box with the WebBrowser control, like ordinary IE usage.
 
Back
Top