Solved

Check if File exists over HTTP

Posted on 2006-07-17
3
288 Views
Last Modified: 2008-03-03
Hi,
I need to find whether a file exists in an Http URL or not.
THe code I am currently using is

private bool ReadGraph(string URL)
    {
        try
        {
            if (Web.OpenRead(URL).CanRead == true)
                return true;
        }
        catch
        {
            return false;
        }
        return true;
    }

This does not work consistently.
Is there a any other approach or am I missing something here.
Any help on this regard is highly appreciated.
Thanks and Regards,
Soni
0
Comment
Question by:hangar18
3 Comments
 
LVL 7

Expert Comment

by:whityum
Comment Utility
open an xml data source

try{
                  XmlDataSource xds = new XmlDataSource();
                  xds.DataFile = URL;
                  xds.DataBind();
                  XmlDocument doc = xds.GetXmlDocument();
                  return true;

}catch{
return false;
}

it will throw an error if there is a 404 for the URL.
0
 
LVL 23

Accepted Solution

by:
apresto earned 125 total points
Comment Utility
Hi hangar18,

Have you considered using the HttpWebRequest/Response objects:

try
{
HttpWebRequest webRequ = WebRequest.Create( "http://www.google.com" ) as HttpWebRequest;
HttpWebResponse webResp = webRequ.GetResponse();
return true; //because url is found
}
catch
{
return false; //because url not found
}

Apresto
0
 
LVL 11

Expert Comment

by:jatinderalagh
Comment Utility
adding to apresto code check for status code

if(webResp.StatusCode== HttpStatusCode.NotFound)
{
   // File not found on server
// i.e file does not exist on web server
}
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now