How to: Check if the specified URL really exists
I think a lot of people need to do this, and we do in our project. We created a method that will check if URLs specified in our database (a couple of hundred) actually exist. Our support team uses the result of that check to update incorrect URLs in our application. I extracted the method and removed some project specific stuff to show it here:
/// <summary>
/// Checks the status of the specified URL.
/// </summary>
/// <param name="url">The URL that needs to be checked</param>
/// <param name="statusText">The status text associated with the specified URL</param>
/// <returns>An Integer value which is the result of the check action</returns>
public int CheckUrl(string url, out string statusText)
{ int status = -1;
statusText = "Unknown status";
try
{
ServerXMLHTTP30Class http = new ServerXMLHTTP30Class();
http.setTimeouts(5000, 5000, 5000, 5000);
http.open("HEAD", url, Missing.Value, Missing.Value, Missing.Value); http.send(Missing.Value);
status = http.status;
// Status 200 specifies the http.send was successful
if (status != 200)
{ statusText = http.statusText;
}
else
{ statusText = "OK";
}
}
catch (COMException com)
{ statusText = com.Message;
}
return status;
}
For most URLs, this method works fine for us. There are some cases where the method fails. For example, some urls (for intranet sites) require a user with sufficient access rights to be logged on. One problem kind of worries us. The http.Send method sometimes throws the following exception:
System.Runtime.InteropServices.COMException (0x80072EE2):
Exception from HRESULT: 0x80072EE2
at MSXML2.ServerXMLHTTP30Class.send(Object varBody)
I'm wondering if someone out there can tell me why, or maybe someone knows of a better (and faster) way to check if a URL exists.