By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,829 Members | 1,829 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,829 IT Pros & Developers. It's quick & easy.

Check File Exists in Web Share (HTTP)

Manikgisl
P: 37
HI.

How to check File exists in Web Share C#
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.     WebRequest request = HttpWebRequest.Create("http://www.microsoft.com/NonExistantFile.aspx");
  4.     request.Method = "HEAD"; // Just get the document headers, not the data.
  5.     request.Credentials = System.Net.CredentialCache.DefaultCredentials;
  6.     // This may throw a WebException:
  7.     using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  8.     {
  9.         if (response.StatusCode == HttpStatusCode.OK)
  10.         {
  11.             // If no exception was thrown until now, the file exists and we 
  12.             // are allowed to read it. 
  13.             MessageBox.Show("The file exists!");
  14.         }
  15.         else
  16.         {
  17.             // Some other HTTP response - probably not good.
  18.             // Check its StatusCode and handle it.
  19.         }
  20.     }
  21. }
  22. catch (WebException ex)
  23. {
  24.     // Cast the WebResponse so we can check the StatusCode property
  25.     HttpWebResponse webResponse = (HttpWebResponse)ex.Response;
  26.  
  27.     // Determine the cause of the exception, was it 404?
  28.     if (webResponse.StatusCode == HttpStatusCode.NotFound)
  29.     {
  30.         MessageBox.Show("The file does not exist!");
  31.     }
  32.     else
  33.     {
  34.         // Handle differently...
  35.         MessageBox.Show(ex.Message);
  36.     }
  37. }
  38.  

i try this above but excpection is thrown while excuting the following Code

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

please Help me

Thanks in Advance

With Regards,
Mani
Jan 6 '09 #1
Share this Question
Share on Google+
2 Replies


P: 1
I know this post is really old, but I experienced a similar problem and I thought it would be worth sharing my experience.

The above code threw an exception in the same place when I tried to test for the existence of a .WSDL document. The error code was a (403) Forbidden. I don't think the error code is important, but what got it to work probably is. When I commented out the request.Method = "HEAD"; no exception was thrown and an existing document was identified.

Based on my experiences, I think it might be fair to say that not all web documents have headers. So, when the above exception is thrown, in some cases, it might be appropriate to test against the whole document, as in the case with .WSDL files.

I hope someone finds this helpful.

-- Tony
Sep 21 '09 #2

Plater
Expert 5K+
P: 7,872
While the HTTP spec says HEAD is a required minimal implementation, I don't believe many servers support it. (Possibly do to some sort of abuse?)
403 forbidden is what normally triggers the popup box in your browser requesting a username/password. It's possible that the server responds with a 403 on HEAD requests, purely for the administrator to do developement work with it.

I don't know how this question slipped by me back in january. The line in question will throw an exception on 404 (file not found) errors, and proabably all 4xx and 5xx statuscodes (I think there is even a setting to make 3xx statuscodes trigger an error). Which confuses me as to what the OPs problem was. They have code in the catch block to determine the status code.
Sep 21 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.