Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

Check if File exists over HTTP

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
hangar18
Asked:
hangar18
1 Solution
 
whityumCommented:
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
 
aprestoCommented:
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
 
jatinderalaghCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now