Reputation: 9555
How do I check if a page exists without wrapping it in a try catch
Dim myWebResponse As HttpWebResponse = CType(myWebRequest.GetResponse(), HttpWebResponse)
If the page does not exist then I get a 404 exeption when myWebRequest.GetResponse(
) fires.
Isnt there some thing like myWebRequest.DoesPageExist() that returns true of false or the status?
Upvotes: 1
Views: 589
Reputation: 18013
Have you tried doing it with ajax? (this example uses jQuery)
$.ajax({
url:'http://www.example.com/somefile.ext',
type:'HEAD',
error: function()
{
//file not exists
},
success: function()
{
//file exists
}
});
Here is the code for checking 404 status, whitout using jquery
function UrlExists(url)
{
var http = new XMLHttpRequest();
http.open('HEAD', url, false);
http.send();
return http.status!=404;
}
taken from:
How do I check if file exists in jQuery or JavaScript?
Upvotes: 1
Reputation: 4578
This is not possible.
To check if a page exists, you have to execute the web request, and the server will return an error code, if the page does not exist. If the page exists, the server returns the page content. All this is one in a single server request, if you would check first, you would need two server requests, which is unefficient.
When you execute a web request, you should always catch exceptions, because lots of unexpected things could happen. Not only that the page does not exists, the connection could timeout or break, the server itself is down.... and you should catch all these exceptions.
Upvotes: 3