473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Check File Exists in Web Share (HTTP)

Manikgisl
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
2 9774
Tony K
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
7,872 Expert 4TB
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

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

Similar topics

5
by: DCK | last post by:
Hello I've path to file, which look like this: \\COMPUTER\D$\C++\FILE_TO_DELETE.JPG This path was generated by os.path.walk() function. When i try to delete this file, os.remove() can't find...
4
by: Rene' Nielsen | last post by:
Context: Running Windows 2003 Server on an intranet. A web is configured with an anonymous access account that is a domain account that has been granted the desired access to a file on another...
6
by: Jennifer Smith | last post by:
Currently we have a site that allows users to listen to mp3 files. It is creating bandwidth issues. So we want to move the mp3 files to an ISP that caps bandwidth usage. Ours is currently...
1
by: Ian | last post by:
I have an application called "Test.exe" and this application has a config file called "Test.exe.config" and it holds the connection string for the database calls in there. To get the...
7
by: andylcx | last post by:
Hi all: The c++ language can check whether the file exist or not. I am wondering how c language does this job? Thanks a lot! Andy
6
by: Chad Crowder | last post by:
Getting the following error on my production server whether the file exists or not: "System.IO.IOException: Cannot create a file when that file already exists." Here's the code generating the...
26
by: ValK | last post by:
Hi, Here is my problem. I need to check if scanner finished scanning the document. I'm using scanner interface that came with the scanner. I tried to open file and catch IO Exception for "The...
0
by: =?Utf-8?B?S2l0dHlIYXdr?= | last post by:
I am trying to use the File.Exists method on image files that are located in a virtual directory on a UNC share from IIS6. The UNC share is located on a Windows 2000 Server machine. I can view...
8
by: Alan | last post by:
Hello, I need to write a systemtray program to check every minute if a certain intranet page exists. Would "webBrowser.Navigate" and catching exceptions to it be a good choice in terms of load...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.