DCSIMG
How to check if a file exists over HTTP - Dor Rotman`s Blog

How to check if a file exists over HTTP

Here’s a piece of C# code that determines the existence of a file over HTTP, given its URL. (Note that URLs should be encoded.)

try
{
    WebRequest request = HttpWebRequest.Create("http://www.microsoft.com/NonExistantFile.aspx");
    request.Method = "HEAD"; // Just get the document headers, not the data.
    request.Credentials = System.Net.CredentialCache.DefaultCredentials;
    // This may throw a WebException:
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        if (response.StatusCode == HttpStatusCode.OK)
        {
            // If no exception was thrown until now, the file exists and we 
            // are allowed to read it. 
            MessageBox.Show("The file exists!");
        }
        else
        {
            // Some other HTTP response - probably not good.
            // Check its StatusCode and handle it.
        }
    }
}
catch (WebException ex)
{
    // Cast the WebResponse so we can check the StatusCode property
    HttpWebResponse webResponse = (HttpWebResponse)ex.Response;
 
    // Determine the cause of the exception, was it 404?
    if (webResponse.StatusCode == HttpStatusCode.NotFound)
    {
        MessageBox.Show("The file does not exist!");
    }
    else
    {
        // Handle differently...
        MessageBox.Show(ex.Message);
    }
}

As you can see, it’s fairly simple – we use the HttpWebRequest class to perform an HTTP request using the verb HEAD.

What is an HTTP HEAD request?

HEAD is similar to GET, only that instead of getting the file contents, we get just the headers. This is what proxies do to: When a proxy server gets a GET request for a URL it has in its local cache, it performs a HEAD request to the real server, to determine the requested file’s date/time stamp. If the file on the server is newer than the cached copy, the proxy will download it and cache it again, and of course – will serve the newer version to the client.

So how does this code work?

If the web request we perform will stumble into a non existing file, a WebException will be thrown: So we just need to catch the WebException and deal with it.

However, consider these two important notes:

1. Don’t assume any WebException indicates a 404 error, check its StatusCode, after casting it to a HttpWebException type. You might get a 500 Internal Server Error or a 401 Unauthorized response when doing web requests, so it’s important to check the error code.

2. As far as I checked, the HttpWebRequest might get redirected to a different URL, and you will not be notified of that. Some sites perform redirects to custom 404 pages instead of regular pages, and would give out a HTTP 200 OK code eventually, since they served the custom 404 page successfully. So check how that website behaves.

You’re welcomed to contribute your own ideas on how to improve this code. This is such a trivial task, yet I found almost no code samples for it online.

Dor Rotman.

Published 02 September 2008 08:22 AM by Dor Rotman

Comments

# manuel du casino said on 21 January, 2009 12:49 PM

Merci bien pour ce code, la synthaxe semble simple et facile d'appliquer.

# Pepijn van de Kamp said on 10 February, 2009 03:24 PM

Please check if the webexception.response is not null. Else your code will break if the dns can't resolve the domain.

# johnson said on 26 April, 2009 06:51 AM

I can't get html code but this page is exsited when you use brower

# RandomDude said on 08 July, 2009 07:59 PM

How'd you go about making it only try for a short while? when i execute this code, it hangs for a few secounds, any way to speed it up?

# ItsOnlyMe said on 08 January, 2011 01:55 PM

Its also best to check the ResponseUri of the HttpWebResponse against the original requested file URL.  

If the web site has security that includes redirecting to a front page the code shows the file exists, when it is this front page that exists for us, not the one we want.

# Verificando se um post Exist por HTTP « Blog do Parmezani said on 25 February, 2011 05:11 PM

Pingback from  Verificando se um post Exist por HTTP « Blog do Parmezani

# First olive garden restaurant recipes said on 20 June, 2011 10:29 AM

Real, <a href= www.gravatar.com/chickensaladrecipess >Buy chicken salad recipes</a>, [url= www.gravatar.com/chickensaladrecipess ]Buy chicken salad recipes[/url],  wtf,

# save the tatas t shirt said on 21 June, 2011 06:42 AM

What?, www.gravatar.com/ihearttshirtsa i heart t shirts,  6733,

# hair color ideas said on 22 June, 2011 12:41 AM

Great, www.gravatar.com/haircolorideasi hair color ideas,  aiqwa,

# color wheel chart said on 22 June, 2011 12:54 AM

Hi, www.gravatar.com/designyourownoutfits design your own outfit discount,  tatuzw,

# nu said on 06 January, 2012 10:01 PM

How about:

private bool existsOnServer(Uri url)

{

var request = HttpWebRequest.Create(url);

request.Method = WebRequestMethods.Http.Head;

Func<HttpWebResponse, bool> exists = response => response.StatusCode != HttpStatusCode.NotFound;

try

{

using (var response = (HttpWebResponse)request.GetResponse())

{

return exists(response);

}

}

catch (WebException ex)

{

return exists((HttpWebResponse)ex.Response);

}

}

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: