Hi

Im tryingto create/run a webbrowser control so that i can check the status. I dont need to see the page so havent placed the webbrowser control on a form - its just in code as follows:

Sub TestBrowse(ByVal url As String)

        Dim testbrowse As New WebBrowser

        testbrowse.Navigate(url)

        System.Threading.Thread.Sleep(5000)

        MsgBox("Status - " & testbrowse.ReadyState.ToString)


    End Sub

the problem is that it never initialises and is always at the state 'Uninitialized'

do i have to do something to the control to get it to run, or does it have to be physically on a form before i can use a web browse control

regards

My advice is to manually insert the control into the form.

Set it to initially not visible, and dynamically (in code) change it to visible state, after invoking Navigate() method.

You can first testing it leaving it visible to test it is working well.


Hope helps.

Since you want only the status code, you'll do well with webbrowser control. Another way to check the code is to use WebRequest and WebResponse.

Here's something I ripped from my app (bugs included). It may be a bit overkill for your purposes (it returns the page HTML etc.) but I'll post the code anyway

Imports System.IO
Imports System.Net
Imports System.Text

' HTTP codes, only the few most common codes are named
Public Enum HTTP_STATUS As Integer
  FAILED = -2
  NOT_SUPPORTED = -1
  UNKNOWN = 0
  APP_TIMED_OUT = 1 ' This application timed out
  NAME_RESOLUTION_FAILURE = 2 ' The Remote Name Could Not Be Resolved
  TRUST_FAILURE = 3 ' The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel
  CONNECTION_CLOSED = 4 ' Connection closed
  OK = 200
  MOVED = 301
  REDIRECT = 302
  BAD_REQUEST = 400
  FORBIDDEN = 403
  PAGE_NOT_FOUND = 404
  REQUEST_TIMEOUT = 408
  INTERNAL_SERVER_ERROR = 500
End Enum

Public Sub GetPage(ByVal URL As String, ByRef PageHTML As String, ByRef StatusCode As HTTP_STATUS, _
  ByRef CharSet As String)
  '
  ' Download page
  '
  Dim HttpWebReq As System.Net.HttpWebRequest
  Dim WebResp As System.Net.HttpWebResponse
  Dim WebStream As System.IO.Stream
  Dim WebReader As System.IO.StreamReader
  'Dim MemStream As System.IO.MemoryStream
  'Dim TempByte() As Byte
  Dim bPageFound As Boolean
  Dim TempStatus As Integer
  '
  'Dim oDetectEncoding As href.Utils.EncodingTools

  PageHTML = ""
  StatusCode = HTTP_STATUS.NOT_SUPPORTED
  TempStatus = -1
  CharSet = ""

  bPageFound = True
  WebResp = Nothing
  Try
    HttpWebReq = CType(WebRequest.Create(URL), HttpWebRequest)
    'HttpWebReq.UserAgent = g_UserAgentStr(g_UserAgentIndex)
    HttpWebReq.Timeout = 20000 'g_TimeOut
  Catch ex As Exception
    HttpWebReq = Nothing
    bPageFound = False
    TempStatus = -2
    Exit Sub
  End Try
  If bPageFound Then
    Try
      WebResp = CType(HttpWebReq.GetResponse(), System.Net.HttpWebResponse)
    Catch ex As WebException
      If ex.Status = WebExceptionStatus.Timeout Then
        TempStatus = ex.Status
      Else
        TempStatus = ex.Status
      End If
      WebResp = Nothing
      bPageFound = False
    Catch ex As Exception
      ' Some error
      If WebResp IsNot Nothing Then
        'StatusCode = CType(WebResp.StatusCode, HTTP_STATUS)
        TempStatus = CType(WebResp.StatusCode, Integer)
      Else
        'StatusCode = HTTP_STATUS.UNKNOWN
        TempStatus = -1
      End If
      WebResp = Nothing
      bPageFound = False
    End Try
    If bPageFound Then
      Try
        'StatusCode = CType(WebResp.StatusCode, HTTP_STATUS)
        TempStatus = CType(WebResp.StatusCode, Integer)
        WebStream = WebResp.GetResponseStream
        If WebStream IsNot Stream.Null Then
          Select Case WebResp.CharacterSet.ToUpper.Trim
            Case "UTF-8"
              WebReader = New System.IO.StreamReader(WebStream, Encoding.UTF8)
            Case Else
              WebReader = New System.IO.StreamReader(WebStream, Encoding.Default)
          End Select
          'ReDim TempByte(Encoding.Default.GetByteCount(WebReader.ReadToEnd))
          'TempByte = Encoding.Default.GetBytes(WebReader.ReadToEnd)
          'MemStream = New MemoryStream(TempByte)
          'WebReader = New System.IO.StreamReader(WebStream, Encoding.UTF8)
          'WebReader = New System.IO.StreamReader(WebStream, Encoding.Default)
          'WebReader = href.Utils.EncodingTools.OpenTextStream(MemStream)
          If WebReader.BaseStream IsNot Nothing Then
            CharSet = WebResp.CharacterSet
            WebResp.ToString()
            ' TODO: use BlockRead and MAX load size to avoid overflow
            PageHTML = WebReader.ReadToEnd
          Else
            PageHTML = ""
            TempStatus = -1
          End If
        Else
          PageHTML = ""
          TempStatus = -1
        End If
      Catch ex As Exception
        PageHTML = ""
      Finally
        WebResp.Close() ' Close finally to free resources
      End Try
    End If
    ' Map web exception code to HTTP status code
    TempStatus = MapWebExceptionStatusToHttpException(TempStatus)
    ' Check that status code is HTTP code
    If IsSupportedStatusCode(TempStatus) Then
      StatusCode = CType(TempStatus, HTTP_STATUS)
    Else
      StatusCode = HTTP_STATUS.NOT_SUPPORTED
    End If
  End If

End Sub

Public Function IsSupportedStatusCode(ByVal value As Integer) As Boolean
  '
  ' Check if value is a supported statuscode

  Try
    Select Case value
      Case HTTP_STATUS.FAILED
        Return True
      Case HTTP_STATUS.NOT_SUPPORTED
        Return True
      Case HTTP_STATUS.UNKNOWN
        Return True
      Case HTTP_STATUS.OK
        Return True
      Case HTTP_STATUS.APP_TIMED_OUT
        Return True
      Case HTTP_STATUS.NAME_RESOLUTION_FAILURE
        Return True
      Case HTTP_STATUS.TRUST_FAILURE
        Return True
      Case HTTP_STATUS.CONNECTION_CLOSED
        Return True
      Case HTTP_STATUS.MOVED
        Return True
      Case HTTP_STATUS.REDIRECT
        Return True
      Case HTTP_STATUS.BAD_REQUEST
        Return True
      Case HTTP_STATUS.FORBIDDEN
        Return True
      Case HTTP_STATUS.PAGE_NOT_FOUND
        Return True
      Case HTTP_STATUS.REQUEST_TIMEOUT
        Return True
      Case HTTP_STATUS.INTERNAL_SERVER_ERROR
        Return True
      Case Else
        Return False
    End Select
    Return False
  Catch ex As Exception
    Return False
  End Try

End Function

Public Function MapWebExceptionStatusToHttpException(ByVal WebExceptionStatusValue As Integer) As Integer
  '
  ' Map exception status value to Http status

  Try
    Select Case WebExceptionStatusValue
      Case 1 ' The Remote Name Could Not Be Resolved
        Return 2
      Case 7 ' Protocol error
        Return 400 ' Bad request
      Case 8 ' Connection
        Return 4
      Case 9 ' The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel
        Return 3
      Case 14 ' Timeout
        Return 1 ' Application timed out
      Case Else
        Return WebExceptionStatusValue
    End Select
  Catch ex As Exception
    Return WebExceptionStatusValue
  End Try

End Function

Public Function MapStatusCodeToString(ByVal StatusCode As Integer) As String
  '
  ' Return textual representation of the statuscode
  '

  Try
    Select Case StatusCode
      Case HttpStatusCode.Forbidden ' 403
        Return "Forbidden"
      Case HttpStatusCode.InternalServerError ' 500
        Return "Internal Server Error"
      Case HttpStatusCode.Moved ' 301
        Return "Moved"
      Case HttpStatusCode.NotFound ' 404
        Return "Page Not Found"
      Case HttpStatusCode.OK ' 200
        Return "OK"
      Case HttpStatusCode.Redirect '302
        Return "Redirect"
      Case HttpStatusCode.BadRequest ' 400
        Return "Bad Request"
      Case HttpStatusCode.RequestTimeout ' 408
        Return "Request Timeout"
      Case -1
        Return "Not Supported Error"
      Case -2
        Return "Failed"
      Case 1
        Return "Application Timeout" ' WebEx = 14
      Case 2
        Return "The Remote Name Could Not Be Resolved" ' WebEx = 1
      Case 3
        Return "The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel" ' WebEx = 9
      Case 4
        Return "Connection Closed" ' WebEx = 8
      Case Else ' 0
        Return "Unknown"
    End Select
  Catch ex As Exception
    Return "N/A"
  End Try

End Function

and here's how you get the status code

Dim PageHTML As String
Dim StatusCode As HTTP_STATUS
Dim PageCharset As String

PageHTML = ""
PageCharset = ""

' Check site's status
GetPage("http://www.daniweb.com", PageHTML, StatusCode, PageCharset)

' Show URL's status
MessageBox.Show("Status: " & MapStatusCodeToString(StatusCode), "HTTP Status", MessageBoxButtons.OK, MessageBoxIcon.Information)

i hope was hoping that i wouldnt have to put it onto a form first. Im using it this way so that i can check the final destination of a link to make sure it is ok beofre directing my proper browser window to it!

solved it - I had to add the application.doevents so that it actually ran the command on the browser - using sleep, as it appears, stops any code from running so it was never giving the browser a chance to load.

The working code for anyones reference is:

Sub TestBrowse(ByVal url As String)

        Dim testbrowse As New WebBrowser

        testbrowse.Navigate(url)

        Do Until testbrowse.ReadyState = WebBrowserReadyState.Complete
            Application.DoEvents()
        Loop

        MsgBox("Status - " & testbrowse.ReadyState.ToString)

    End Sub

Seems fine to me. I would, however, put some extra check in the loop i.e. some way to "time out" from the loop if the ReadyState never returns Complete. I'm not sure if that could happen but if it happens, you'll end up to endless loop.

good point - il look into it

many thanks for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.