Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives

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.

Comments

Matthijs said:

I don't know about the COM exception, but I think you can avoid the use of ServerXMLHTTP30Class altogether using the following code:

HttpWebResponse response;

WebRequest request = HttpWebRequest.Create("http://www.bloggingabout.net");

request.Timeout = 5000;

try

{

   response = (HttpWebResponse)request.GetResponse();

}

catch (WebException wex)

{

   response = (HttpWebResponse)wex.Response;

}

Console.WriteLine("{0} - {1}: {2}",

  (int)response.StatusCode,

  response.StatusCode,

  response.StatusDescription);

# December 12, 2007 1:53 PM

Jan Schreuder said:

I know this solution, but we abandoned it because it leans pretty heavy on the Exceptions thrown and it requires the server to render the entire request including rendering of the images. The ServerXMLHTTP30Class merely retrieves the response header. We did checks between both options and the ServerXMLHTTP30Class was faster.

# December 12, 2007 2:14 PM

Matthijs said:

You can add 1 line to only request the headers using the HttpWebRequest object, just like you do with [http.open("HEAD"...].

This should be just as fast:

request.Method = WebRequestMethods.Http.Head;

# December 12, 2007 2:32 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)