Sunday, September 12, 2010

Checking if a Remote (HTTP) File Exists

If you are going to be performing any action on a file, you want to make sure the file exists first. This is easier for files stored locally on your machine where you can simply do a File.Exists call and...call it a day.
Things are a little bit more tricky when you try to check if a remote file such as something on a HTTP server exists though. Given the nature of requests traveling over a network, there are two things to keep in mind that make performing this check non-trivial:
You want to make sure your application remains performant while the network request is being made.
You do not want to download the entire remote file when checking if it exists.
Given those two requirements, you have to do a little more work than just using a simple File.Exists call. Instead, you have to construct a web request yourself and listen for the appropriate response to verify that the remote file exists.
In this article, since it is more of a deconstruction as opposed to a tutorial, below you will find a project containing the code for both creating and sending the web request and listening for the response
Read more: Kirupa.com