Hi,
I'm building a small program which displays images that are downloaded from a website during runtime.
My question is:
How do you determine whether a file exists on the web (i.e. check to see if a .gif file exisist at a URL e.g. "http://www.somedomain.com/someimagename.gif")

Web file existist
Natarajan Arumugam
Hi,
Iv'e had a look over the above code and it makes some sense to me. I think it will work. However, when I copied the code over and tried to compile it, it compains about the type or namespace could not be found.
Can you help me with this
Thanx.
P.S. (sorry about the delay in getting back to you, went to bed)
Steve001
Cool, thanx.
Got the basics going now so I should be fine.
Thanx for your help
mduffin
Hi,
Well this is working fine now when the image exisist at location specified but when it does not the program just crashes.
What is the exception I need to catch or something
Thanx
blaflen
If it's expected behaviour to not find a file on the server it would be nice if no exception would be thrown. Is there a way to accomplish this
Thanks,
Neno
KyleT
You'll need to make sure you've got the following using statements at the top of your code file:
using System.Net;
using System.IO;
using System.Drawing;
Dr. NO
You can change your catching to:
catch (WebException ex)
{
//Request failed, only ex can tell us why
}
catch (Exception ex)
{
//Some other problem occured
}
to not only specifically catch an exception generated by the request, but also any other and be able to act on each separately.
Swooter
Really the only way to determine if the file is there is to request it and check the status code you get back and/or check the full output as well.
A simple way to check the status code is:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.somedomain.com/someimagename.gif");
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
//URL found
}
response.Close();
}
catch (WebException ex)
{
//Request failed, only ex can tell us why
}
In this, we create a request for a given file on a given server and issue the request with GetResponse(). Next, we check the response’s status code to see if is the HTTP code for OK. If it is, we assume that a file exists on the server that the server gives for that URL.
One drawback of this method is that it does not guarentee that the file in question is of the type you want, for all we know the server may redirect you to another page without actualy returning the File Not Found code (404).
If you want to verify the data that is being received back, use response.GetResponseStream() to construct a StreamReader with which to create a new image within the if block ala:
StreamReader sr = new StreamReader(response.GetResponseStream());
Bitmap b = new Bitmap(response.GetResponseStream());
If the data you receive is not able to be made into an image the .NET Framework understands, this will throw an exception (which you will still need to catch), otherwise you’ve verified that the URL exists and that an image is being served up for requests to it.
Does this help you out