Link to home
Create AccountLog in
Avatar of davie455
davie455

asked on

Visual Basic WebBrowser Control Navigation

Hi Experts,

I have had not interest in my question asked here and I'm not sure why....
https://www.experts-exchange.com/questions/22610713/Simple-vb-net-project-with-WebBrowser-control-Custom-HTTP-Headers-Printing.html

None the less let me refocus the question for the same pointage.

I use Visual Basic.net to load a WebBrowser Control into a form, the control navigates to a certain page on load and passes my custom HTTP headers as it does so, but what I am struggling with is getting the custom headers included on every naviagate. (navigation would only ever be within my own asp application).

My code so far is below...I think part of the problem may be to do with framesets.  The page that loads with the form is a single frame (a login page) which then redirect to a left and main frame.

Pleas help me.





Public Class Form1

    Dim myByte(0) As Byte
    Dim urlParse As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Me.urlParse = 0

        WebBrowser1.Navigate("http://www.google.co.uk")


    End Sub




    Private Sub WebBrowser1_Navigating(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating

        If Me.urlParse = 0 Then

             //HEADERS NOT ADDED, CANCEL NAVIGATING AND CALL NAVIGATE2
            e.Cancel = True
            Me.Navigate2(e.Url, e.TargetFrameName)

        Else
             //HEADERS ALREADY ADDED...DO NOTHING AND NAVIGATION CONTINUES

        End If

    End Sub

    Dim urlString As String
    Dim frameString As String

    Public Sub Navigate2(ByVal urlString As Uri, ByVal frameString As String)


        Me.urlParse = 1


        Dim HWS As String
        Dim SWS As String




        HWS = "HWS"
        SWS = "SWS"

        myByte(0) = 1

        WebBrowser1.Navigate(urlString, frameString, myByte, "SWS: " & SWS & vbCrLf & "HWS: " & HWS & vbCrLf)


    End Sub







    Private Sub WebBrowser1_Navigated(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated

         Me.urlParse = 0


    End Sub





End Class
Avatar of dodieguy
dodieguy


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim url As String
        url = "http://www.d-s-s.nl"
        browser.Navigate(New Uri(url))
    End Sub


try this instead
Avatar of davie455

ASKER

Thanks dodieguy, I was beginning to lose faith.

Does this capture mouse clicks, as in, somebody clicks a link within the HTML of the WebBrowser control and this sub captures the arguments including the URL of the link and allows me to add me headers?

I have tried to implement but...

 Error      1      Handles clause requires a WithEvents variable defined in the containing type or one of its base types.      

Thanks
i can gice you the code of a working (simple ) browser..
all you need is:
1 textbox called toolStripTextBox1
1 buttun called gobutton
1 webbrowser called webrowser1

this sould work


Public Class Form1

    ' Navigates to the URL in the address box when
    ' the ENTER key is pressed while the ToolStripTextBox has focus.
    Private Sub toolStripTextBox1_KeyDown( _
        ByVal sender As Object, ByVal e As KeyEventArgs) _
        Handles toolStripTextBox1.KeyDown

        If (e.KeyCode = Keys.Enter) Then
            Navigate(toolStripTextBox1.Text)
        End If

    End Sub

    ' Navigates to the URL in the address box when
    ' the Go button is clicked.
    Private Sub goButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles gobutton.Click

        Navigate(toolStripTextBox1.Text)

    End Sub

    ' Navigates to the given URL if it is valid.
    Private Sub Navigate(ByVal address As String)

        If String.IsNullOrEmpty(address) Then Return
        If address.Equals("about:blank") Then Return
        If Not address.StartsWith("http://") And _
            Not address.StartsWith("https://") Then
            address = "http://" & address
        End If

        Try
            WebBrowser1.Navigate(New Uri(address))
        Catch ex As System.UriFormatException
            Return
        End Try

    End Sub

    ' Updates the URL in TextBoxAddress upon navigation.
    Private Sub webBrowser1_Navigated(ByVal sender As Object, _
        ByVal e As WebBrowserNavigatedEventArgs) _
        Handles WebBrowser1.Navigated

        toolStripTextBox1.Text = WebBrowser1.Url.ToString()
        WebBrowser1.Size = Me.Size

    End Sub


    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        WebBrowser1.Size = Me.Size
    End Sub

 
End Class
Thanks for the code, simple and complete.

But this is not what I am looking for here...essentially the browser only views my application on my server, no address bar, no buttons, context disabled as well as keyboard short cuts....all a user can do is open a menu less window which takes them to my URL and navigate from there through whatever navigation is possible in the HTML.  navigation usually just fires from links dropdowns and form submit.

I can get my code to work on form load but cannot find a way to get the headers to persist.  I considered simply catching the headers (from form load navigation) on my server and storing in session but when session expires, unless the custom headers are present then instead of redirecting to login I want to redirect to 'go away I can't see the headers I'm expecting.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of dodieguy
dodieguy

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
dodie guy apologies for the delay, although not solving my original problem you have been helpful and suggested an alternative.  I had considered this alterntive before but it wasn't my preferred way of solving my problem.


If you could quickly point me towards what I'm doing wrong with the below that would be great...

on Client request #1 What I would like to do is as soon as the ASP session cookie is created (before Client request #2 is made containing the ASP session cookie in it's HTTP header) I would like to write to DB the session cookie but don't seem to be able to do this.  Session.SessionID does not match the HTTP_COOKIE header returned by the client.

Many Thanks