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
I did the following and it worked:
Note that I also added some headers to help normalize the request. Without those the particular server didn't allow the request.
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