locked
Catch 404 error exception on web browser control RRS feed

  • Question

  • How can I catch 404 error on web browser control?

     

     Try
      TimeTable.Navigate("URL/File.htm")
     Catch ex As Exception
      TimeTable.Navigate("URL/File2.html")
    End Try
    

    It still show 404 error on the control.



    Saturday, May 28, 2011 6:48 AM

Answers

All replies

  • How can I catch 404 error on web browser control?

     

     Try
    
    
    
     TimeTable.Navigate("URL/File.htm")
    
    
    
     Catch ex As Exception
    
    
    
     TimeTable.Navigate("URL/File.html")
    
    
    
    End Try
    
    
    
    

    It still show 404 error on the control.



    Hello duanebytes,

    The 404 error indicates that we could not find the requested URL, try typing another URL to see if a problem
    code or actually find the page you want does not exist.

    Try
    
     TimeTable.Navigate("URL\File.htm")
    
     Catch ex As Exception
    
     TimeTable.Navigate("URL\File.html")
    
    End Try
    
    
    
    

    Hello


    Carmelo La Monica  http://community.visual-basic.it/carmelolamonica/
    Saturday, May 28, 2011 6:58 AM
  • How can I catch 404 error on web browser control?

     

     Try
    
    
    
     TimeTable.Navigate("URL/File.htm")
    
    
    
     Catch ex As Exception
    
    
    
     TimeTable.Navigate("URL/File.html")
    
    
    
    End Try
    
    
    
    

    It still show 404 error on the control.



    Hello duanebytes,

    The 404 error indicates that we could not find the requested URL, try typing another URL to see if a problem
    code or actually find the page you want does not exist.

    Try
    
     TimeTable.Navigate("URL\File.htm")
    
     Catch ex As Exception
    
     TimeTable.Navigate("URL\File.html")
    
    End Try
    
    
    
    

    Hello


    Carmelo La Monica  http://community.visual-basic.it/carmelolamonica/

    I'm sorry. In my post above. after the Catch. The file name should be a different name. File2.html. Sorry my bad.

    Fixed in the first post.



    And I'm sure that File2.html exists.
    Saturday, May 28, 2011 7:02 AM
  • You get the 404 returned from a webserver, it is not really an error which you can catch, because the navigate function is doing it correct.

     


    Success
    Cor
    Saturday, May 28, 2011 8:13 AM
  • Hello,

    the  HTTP Status Codes are realised on http network protocol. You cannot catch them as exception.

    But You can handle the errors on this way:

    Dim response As HttpWebResponse = request.GetResponse() 
     If response.StatusCode = HttpStatusCode.OK Then

     Sorry I've overlooked something. webbrowser control:

    gets the status of the browser :StatusTextChanged

    regards Ellen


    Ich benutze/ I'm using VB2008 & VB2010
    Saturday, May 28, 2011 11:17 AM

  • (I appologize if this is a duplicate. I thought I posted a while ago...)
     
    Try the following translated from an old C# post on the same question:
     
    Option Strict On
    Public Class Form1
     Private Sub Button1_Click _
      (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      WebBrowser1.Navigate("
    http://localhost/foo.bar")
     End Sub
     Private Sub ErrorHandler(ByVal pDisp As Object, ByRef URL As Object, ByRef Frame As Object,
      ByRef StatusCode As Object, ByRef Cancel As Boolean)
      If (StatusCode.ToString() = "404") Then
       MessageBox.Show("404...")
      End If
     End Sub
     Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
      Dim axBrowser As SHDocVw.WebBrowser
      axBrowser = CType(WebBrowser1.ActiveXInstance, SHDocVw.WebBrowser)
     
      AddHandler axBrowser.NavigateError, AddressOf ErrorHandler
     End Sub
    End Class

    --
    Mike
    • Proposed as answer by TnTinMN Thursday, June 2, 2011 4:28 AM
    Saturday, May 28, 2011 11:55 AM
  • Mike,

    I just try Your code. I cannot reach the NavigateError Event.

    Handles WebBrowser1.  ... this event not displayed

    Ellen


    Ich benutze/ I'm using VB2008 & VB2010
    Saturday, May 28, 2011 1:13 PM

  • Do you have a webbrowser control named WebBrowser1 on your form?

    --
    Mike
    Saturday, May 28, 2011 1:32 PM

  • Sorry Ellen, I neglected to mention you need to add a COM reference to "Microsoft Internet Controls".

    --
    Mike
    Saturday, May 28, 2011 1:42 PM
  • Mike, I have this webbrowser control:

     Friend WithEvents WebBrowser1 As System.Windows.Forms.WebBrowser
    
    Ellen
    Ich benutze/ I'm using VB2008 & VB2010
    Saturday, May 28, 2011 2:02 PM
  • Mario Cossi made an interesting observation regarding the DocumentType at

    http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/3557dd12-252b-49be-9572-1d647eb36991/

    "As for the detection of errors, the documentation offers no direct solution to the problem. Yet, there might be an undocumented workaround: the DocumentType property is "HTML document" when a page is displayed, but becomes "" whenever the page or the server do not exist. This seems to be true even if the server redirects you to a custom 404 page."

    I tried checking it in the DocumentCompleted event, and it does seem to work.

    Addendum: I switched off IIS on my machine, and got "File" for the DocumentType when trying to navigate to a page on 127.0.0.1.

    --
    Andrew


    • Edited by Andrew Morton Saturday, May 28, 2011 3:52 PM addendum
    Saturday, May 28, 2011 2:38 PM
  • Hello,

    the  HTTP Status Codes are realised on http network protocol. You cannot catch them as exception.

    But You can handle the errors on this way:

    Dim response As HttpWebResponse = request.GetResponse() 
     If response.StatusCode = HttpStatusCode.OK Then

     Sorry I've overlooked something. webbrowser control:

    gets the status of the browser :StatusTextChanged

    regards Ellen


    Ich benutze/ I'm using VB2008 & VB2010
    Private Sub WB_StatusTextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WB.StatusTextChanged
            Dim response As HttpWebResponse = request.GetResponse()
            If response.StatusCode = HttpStatusCode.OK Then
            Else
                WB.Navigate("URL/File2.html")
            End If

        End Sub
    Does not work

    Saturday, May 28, 2011 4:36 PM
  • Hello,

    HttpWebResponse is anoher technique. Please try this code:

    Private Sub WB_StatusTextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WB.StatusTextChanged
    
    Me.label1.Text = WB.StatusText
    
      End Sub
    
    

    Create a new label on the form to display text. Does the event fire?

    regards Ellen

     


    Ich benutze/ I'm using VB2008 & VB2010
    Sunday, May 29, 2011 12:06 PM
  • Hello,

    HttpWebResponse is anoher technique. Please try this code:

    Private Sub WB_StatusTextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WB.StatusTextChanged
    
    Me.label1.Text = WB.StatusText
    
     End Sub
    
    

    Create a new label on the form to display text. Does the event fire?

    regards Ellen

     


    Ich benutze/ I'm using VB2008 & VB2010
    Sorry for the late reply, was busy. Tried what you suggested, the label.text changes from "Label1" to ""
    Thursday, June 2, 2011 1:21 AM
  • Hello,

    I've found a soulution. It works. Make a new Windows forms project, no changes in formdesigner and copy the code into the emtpy code window.

    success and best regards Ellen

    http://social.msdn.microsoft.com/Forums/de-DE/visualbasicde/thread/292445fe-cd1c-450d-8ab8-5dd9cc1fbccd/#d068e8c1-8f33-4efd-8b94-13ebb1b139f0

    link to CreateSink Method


    Ich benutze/ I'm using VB2008 & VB2010
    • Edited by Ellen Ramcke Tuesday, June 7, 2011 6:25 AM link
    • Marked as answer by Tidak Aktif Saturday, June 11, 2011 4:24 PM
    Monday, June 6, 2011 2:52 PM
  • Hello,

    I've found a soulution. It works. Make a new Windows forms project, no changes in formdesigner and copy the code into the emtpy code window.

    success and best regards Ellen

    http://social.msdn.microsoft.com/Forums/de-DE/visualbasicde/thread/292445fe-cd1c-450d-8ab8-5dd9cc1fbccd/#d068e8c1-8f33-4efd-8b94-13ebb1b139f0

    link to CreateSink Method


    Ich benutze/ I'm using VB2008 & VB2010

    That's quite a long line of code. I'll try to understand and post results soon.

    And wb = Web browser control right?

    Tuesday, June 7, 2011 4:30 PM
  • And wb = Web browser control right?


    Hello,

    Yes it's ok. Take a look to this line:

    Private WithEvents wb As New WebBrowser2()

    regards Ellen

     


    Ich benutze/ I'm using VB2008 & VB2010
    Tuesday, June 7, 2011 6:16 PM
  • And wb = Web browser control right?


    Hello,

    Yes it's ok. Take a look to this line:

    Private WithEvents wb As New WebBrowser2()

    regards Ellen

     


    Ich benutze/ I'm using VB2008 & VB2010

    Thanks Ellen.


    • Marked as answer by Tidak Aktif Saturday, June 11, 2011 4:24 PM
    • Unmarked as answer by Tidak Aktif Saturday, June 11, 2011 4:31 PM
    Saturday, June 11, 2011 4:24 PM
  • If the page is valid, it doesn't navigate to the site. Help?
    Oh I have found what my problems are:

    In the Load event there's this code:

    Controls.Add(wb)

    I also added a WebBrowser control with the name WebBrowser2 so it kinda replaces the Web Browser.

    It works now.

    Sunday, June 12, 2011 8:25 AM
  • How can I do this if the controls are already on my form? I got this error:

    'HomeworksBrowser' is already declared as 'Friend WithEvents HomeworksBrowser As System.Windows.Forms.WebBrowser' in this class.

    Wednesday, July 6, 2011 2:10 PM
  • Hmm,

    Private WithEvents wb As New WebBrowser2()

    does not make a instance of webBrowser. But the webBrowser2 is inherits from webBrowser.

    It seems; that You make second instance by draging the control on to the form?

     


    Ich benutze/ I'm using VB2008 & VB2010
    Wednesday, July 6, 2011 2:59 PM
  • Hmm,

    Private WithEvents wb As New WebBrowser2()

    does not make a instance of webBrowser. But the webBrowser2 is inherits from webBrowser.

    It seems; that You make second instance by draging the control on to the form?

     


    Ich benutze/ I'm using VB2008 & VB2010
    Yes. The web browser are already on my form.
    Saturday, July 9, 2011 2:27 AM