Solved

Check if File exists over HTTP

Posted on 2006-07-17
3
291 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

20 Experts available now in Live!

Get 1:1 Help Now