1

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?

user4593252
  • 3,496
  • 6
  • 29
  • 55
  • I did say that the page to test is inside an MVC site. The calling code technology is 100% irrelevant as I can implement it any way necessary. – user4593252 Jul 17 '17 at 19:32
  • Sorry didn't get your question til now, did you try using `UrlExists()`? Check [this](https://stackoverflow.com/questions/12957295/asp-net-check-if-url-exists-before-opening-popup) out. – Capn Jack Jul 17 '17 at 19:34
  • Can't you just return an empty response? I don't think I completely understand this. The server is going to have to return something for you to know if the url is valid. – smoyer Jul 17 '17 at 19:35
  • 1
    No, you cannot control from outside how the server will process your request (with HEAD or not) and the only way to know if an URL is valid is to make a request to that URL – Sir Rufo Jul 17 '17 at 19:44

1 Answers1

1

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!

Ckratide
  • 368
  • 1
  • 8