Solved

Check if File exists over HTTP

Posted on 2006-07-17
3
294 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
ID: 17123041
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
ID: 17123045
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
ID: 17126392
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

825 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