- I want to check whether the file in an url entered exists or not in .net
- How can I check if an Image exists at http://someurl/myimage.jpg in C#/ASP.NET
So I took the best of both of these posts and created the following helper method.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static bool UrlExists(string url) | |
{ | |
try | |
{ | |
var request = WebRequest.Create(url) as HttpWebRequest; | |
if (request == null) return false; | |
request.Method = "HEAD"; | |
using (var response = (HttpWebResponse)request.GetResponse()) | |
{ | |
return response.StatusCode == HttpStatusCode.OK; | |
} | |
} | |
catch (UriFormatException) | |
{ | |
//Invalid Url | |
return false; | |
} | |
catch (WebException) | |
{ | |
//Unable to access url | |
return false; | |
} | |
} |
Please feel free to modify this if you find any issues or problems with.