asp.net.netvb.neturl

.Net test URL is valid?


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

Solution

  • 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.