23

In my application I use the WebClient class to download files from a Webserver by simply calling the DownloadFile method. Now I need to check whether a certain file exists prior to downloading it (or in case I just want to make sure that it exists). I've got two questions with that:

  1. What is the best way to check whether a file exists on a server without transfering to much data across the wire? (It's quite a huge number of files I need to check)
  2. Is there a way to get the size of a given remote file without downloading it?

Thanks in advance!

Mats
  • 14,902
  • 33
  • 78
  • 110

4 Answers4

45

WebClient is fairly limited; if you switch to using WebRequest, then you gain the ability to send an HTTP HEAD request. When you issue the request, you should either get an error (if the file is missing), or a WebResponse with a valid ContentLength property.

Edit: Example code:

WebRequest request = WebRequest.Create(new Uri("http://www.example.com/"));
request.Method = "HEAD";

using(WebResponse response = request.GetResponse()) {
   Console.WriteLine("{0} {1}", response.ContentLength, response.ContentType);
}
Jeroen K
  • 10,258
  • 5
  • 41
  • 40
Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
  • Thanks for your answer! I've seen that I can get a response through the GetResponse() method and then check the ContentLength. But does this make sure the entire file is not downloaded? I can't find a way to send an HTTP HEAD request. Could you point me into the right direction? – Mats May 06 '09 at 16:40
  • @Matthias Create a WebRequest with WebRequest.Create(uri) and then set the 'Method' property to "HEAD". – chakrit May 06 '09 at 16:55
  • What chakrit said; also, see example. – Tim Robinson May 06 '09 at 16:58
  • Edited to use 'using' to avoid timeouts http://stackoverflow.com/questions/2022021/httpwebrequest-allowautoredirect-false-can-cause-timeout – Jeroen K Apr 12 '13 at 08:31
  • Some websites don't allow "HEAD" (Amazon.com for example). To fix this you could surround first request with try and catch, and then if an exception comes up - catch it and try again with the use of "GET" method. Although, I'm not sure, if there are more websites not accepting "HEAD", then maybe it makes sense just to use the "GET" method. – Arman Bimatov Oct 14 '13 at 13:27
5

When you request file using the WebClient Class, the 404 Error (File Not Found) will lead to an exception. Best way is to handle that exception and use a flag which can be set to see if the file exists or not.

The example code goes as follows:

System.Net.HttpWebRequest request = null;
System.Net.HttpWebResponse response = null;
request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("www.example.com/somepath");
request.Timeout = 30000;
try
{
    response = (System.Net.HttpWebResponse)request.GetResponse();
    flag = 1;
}
catch 
{
    flag = -1;
}

if (flag==1)
{
    Console.WriteLine("File Found!!!");
}
else
{
    Console.WriteLine("File Not Found!!!");
}

You can put your code in respective if blocks. Hope it helps!

Code
  • 99
  • 1
  • 4
0

What is the best way to check whether a file exists on a server without transfering to much data across the wire?

You can test with WebClient.OpenRead to open the file stream without reading all the file bytes:

using (var client = new WebClient()) 
{
    Stream stream = client.OpenRead(url); 
    // ^ throws System.Net.WebException: 'Could not find file...' if file is not present
    stream.Close(); 
}

This will indicate if the file exists at the remote location or not.

To fully read the file stream, you would do:

using (var client = new WebClient()) 
{
    Stream stream = client.OpenRead(url); 
    StreamReader sr = new StreamReader(stream);
    Console.WriteLine(sr.ReadToEnd());
    stream.Close(); 
}
SNag
  • 17,681
  • 10
  • 54
  • 69
0

In case anyone stuck with ssl certificate issue

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback
                                                                            (
                                                                               delegate { return true; }
                                                                            );
            WebRequest request = WebRequest.Create(new Uri("http://.com/flower.zip"));
            request.Method = "HEAD";

            using (WebResponse response = request.GetResponse())
            {
                Console.WriteLine("{0} {1}", response.ContentLength, response.ContentType);
            }
Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87