Check File Exists in Web Share (HTTP)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Manikgisl
    New Member
    • Oct 2008
    • 37

    #1

    Check File Exists in Web Share (HTTP)

    HI.

    How to check File exists in Web Share C#
    [code=c#]
    try
    {
    WebRequest request = HttpWebRequest. Create("http://www.microsoft.c om/NonExistantFile .aspx");
    request.Method = "HEAD"; // Just get the document headers, not the data.
    request.Credent ials = System.Net.Cred entialCache.Def aultCredentials ;
    // This may throw a WebException:
    using (HttpWebRespons e response = (HttpWebRespons e)request.GetRe sponse())
    {
    if (response.Statu sCode == 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 = (HttpWebRespons e)ex.Response;

    // Determine the cause of the exception, was it 404?
    if (webResponse.St atusCode == HttpStatusCode. NotFound)
    {
    MessageBox.Show ("The file does not exist!");
    }
    else
    {
    // Handle differently...
    MessageBox.Show (ex.Message);
    }
    }
    [/code]


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

    using (HttpWebRespons e response = (HttpWebRespons e)request.GetRe sponse())

    please Help me

    Thanks in Advance

    With Regards,
    Mani
  • Tony K
    New Member
    • Sep 2009
    • 1

    #2
    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

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      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.

      Comment

      Working...