Reputation: 3506
I want to determine if a URL is valid without firing the server response of returning the page if it is. (ie: without consuming the web page).
I've found this very clever attempt but it doesn't seem to work at least on an MVC action without firing the action itself (which, incidentally, uses Selenium WebDriver and starts a FirefoxDriver instance which is how I know that the page is being consumed.)
Is there a way to say "Hey, server, I don't want the page but does it exist?" without the server actually trying to hand back the page?
Upvotes: 1
Views: 65
Reputation: 368
You can send the http "HEAD" command (instead of "GET", "POST", etc...) to just return the page headers. The server should return a 200 if the page exists, and a 404 if it doesn't.
Something like this:
HttpClient httpClient = new HttpClient();
HttpRequestMessage request =
new HttpRequestMessage(HttpMethod.Head,
new Uri("http://www.google.com"));
HttpResponseMessage response = await httpClient.SendAsync(request);
Good luck!
Upvotes: 1