How To Validate Links Using ASP

Written by Nick Dunn published 8th Mar 2006 | Comment on this article

Using the XMLHTTP object we can programmatically check the status of a specific URL. This can be useful for checking if files exist on your website.

How To Validate Links Using ASP

Sometimes you may wish to check whether a specific link is working or not. Maybe you want to check to see if the server is responding, or if a file exists. Well now you do this from directly within your ASP code!

<%
Function URLStatus(strURL)
Dim objXMLHTTP, strReturnStatus
Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")

objXMLHTTP.Open "GET", "http://www.openhosting.co.uk/default.asp", False
objXMLHTTP.Send

strReturnStatus = objXMLHTTP.Status
Set objXMLHTTP = Nothing

URLStatus = strReturnStatus
End Function

Dim strfrmURL
strfrmURL = CStr(Request.Form("frmURL"))
If strfrmURL <> "" Then

If URLStatus(strfrmURL) = "200" Then
Response.Write "Link is working."
Else
Response.Write "Link is not working: " & URLStatus(strfrmURL)
End If

End If
%>

This function receives a parameter in the form of a URL or IP address. It uses the XMLHTTP object and opens the URL. A returned value of ?200? means the link is successful (server responds and/or file exists) and a value of anything else is specific to the error. For example, if the file does not exist then ?404? will be returned.