skip to Main Content

We have a form on our website that asks customers to enter their website address.

I currently have the code below which checks that their URL is alive.

The code works most of the time. However I recently saw it failed a URL giving the exception Permanently Moved (302) even though entering the URL in a browser address bar worked.

The URL entered on the form was tentworld.com.au

Is there a way to return that such sites URL’s are OK?

   Private Function fCheckUrlIsOK(strURL As String) As Boolean

    strURL = strURL.Trim.ToLower

    'Prefix http if not there already
    If Left(strURL, 7) = "http://" Or
       Left(strURL, 8) = "https://" Then

    Else
        strURL = "http://" + strURL
    End If

    Try
        Dim request1 As HttpWebRequest = WebRequest.CreateHttp(strURL)
        With request1
            .Timeout = 10000 '10 seconds
        End With

        Dim response1 As WebResponse = request1.GetResponse()

    Catch ex As Exception

        'Uncomment to see exception
        'fErrorRecord(ex)

        Return False

    End Try

    Return True

End Function

2

Answers


  1. Chosen as BEST ANSWER

    I did the following and it worked:

      Private Function fCheckURLIsOK(strURL As String) As Boolean
    
        Dim strURLCleaned As String = strURL.ToLower.Trim
        Dim strURLNew As String = ""
    
        'Remove prefixes
        strURLCleaned = strURLCleaned.Replace("https://www.", "")
        strURLCleaned = strURLCleaned.Replace("https://", "")
        strURLCleaned = strURLCleaned.Replace("http://www.", "")
        strURLCleaned = strURLCleaned.Replace("http://", "")
    
        'Try 1
        strURLNew = "https://www." + strURLCleaned
        If fCheckUrlIsOKIndividual(strURLNew) = True Then
            Return True
        End If
    
        'Try 2
        strURLNew = "https://" + strURLCleaned
        If fCheckUrlIsOKIndividual(strURLNew) = True Then
            Return True
        End If
    
        'Try 3
        strURLNew = "http://www." + strURLCleaned
        If fCheckUrlIsOKIndividual(strURLNew) = True Then
            Return True
        End If
    
        'Try 4
        strURLNew = "http://" + strURLCleaned
        If fCheckUrlIsOKIndividual(strURLNew) = True Then
            Return True
        End If
    
        Return False
    
    End Function
    
    Private Function fCheckUrlIsOKIndividual(strURL As String) As Boolean
    
        Try
            Dim request1 As HttpWebRequest = WebRequest.CreateHttp(strURL)
            With request1
                .Timeout = 10000 '10 seconds
            End With
    
            Dim response1 As WebResponse = request1.GetResponse()
    
        Catch ex As Exception
    
            'Uncomment to see exception
            'fErrorRecord(ex)            
    
            Return False
    
        End Try
    
        Return True
    
    End Function
    

    Note that I also added some headers to help normalize the request. Without those the particular server didn't allow the request.


  2. You can try using the HttpClient class. You can check the status code of the response without it throwing an exception for a 404 status code. It is really designed to be used asynchronously but you could try something like this

    Private Function fCheckUrlIsOK(strURL As String) As Boolean
    
        Try
            Dim t = Task.Run(Function() As Boolean
                     Return CheckStatusCode(strURL)
                 End Function)
            t.Wait()
            Return t.Result
    
        Catch ex As Exception
    
            'Can still generate exceptions but not for 400s
            'fErrorRecord(ex)
    
            Return False
    
        End Try
    
        Return True
    
    End Function
    
    Private Async Function CheckStatusCode(strURL As String) As Boolean
    
        Using client As HttpClient = New HttpClient()
            Using response As HttpResponseMessage = Await client.GetAsync(strURL)
                Return response.IsSuccessStatusCode()
            End Using
        End Using
    End Function
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search