0

I need to create a reliable test to see if the current host can reach the public internet using HTTP (port 80) or HTTPS (port 443).

This is not a programming language question. I got that part covered.

My idea is that I'll test against something on the public internet that

  1. Is always alive

  2. Can respond to HTTP or HTTPS requests

  3. Can respond fast (I'm thinking I'll do a HEAD request to keep the reply small)

  4. Will exist also in a forseable future

What would that endpoint be? Is there some official server that can make the above promises, e.g. something provided by w3.org ? (I know that people tend to use www.google.com or similar but doesn't sound to me to be a good solution, or?..).

Further my thinking is:

  • Not to use something like a ping test because ICMP is blocked by many firewalls.

  • Probably needs to be a true HTTP/HTTPS request in order to go through a potential firewall. A plain socket test might give false result if there's a firewall in-between. So I'll do a true HTTP/HTTPS test. My HTTPS test will ignore certs because that's not what I want to test.

  • No matter what I do I'll also indirectly be testing name resolution (unless I'm testing against an IP address which I find unlikely). This is ok. Name resolution is in any case also part of "internet connectivity".

What would be the most reliable way to perform an internet connectivity test (I only care about HTTP and HTTPS) and which endpoint should I use ?

I'm assuming that thousands of developers before me have tackled this. But how?

UPDATE AND FINAL SOLUTION

It seems I will not get anyone answering with an official endpoint to use for this purpose (perhaps there just isn't one?) so what I'll do is to send a HTTP HEAD request to google.com. My request will have HTTP header element cache-control: no-cache hoping to avoid any in-between proxies answering me from their own cache. I'll then repeat exactly the same test for HTTPS.

There's no such thing as a perfect test but from my requirements the above is what seems to come closest.

Thanks to anyone who has answered.

peterh
  • 18,404
  • 12
  • 87
  • 115
  • "This is not a programming language question" means it is not probably a question for stackoverflow, maybe [superuser](http://superuser.com/) might be more helpful. Whats wrong with google.com anyway? Dont thing there will be a site with better availability. Maybe try to check multiple sites in case first one fails. – Milan Halada May 21 '14 at 07:34
  • Make a pastebin with the contents of "hello" and request the html of that. – TheBrenny May 21 '14 at 07:55
  • And of course, the fact that you can reach `google.com` doesn't necessarily mean you're getting through to the internet - you might very well be talking with a proxy somewhere. At least do a proper google search request, on something weird and random :) – Luaan May 21 '14 at 08:21
  • You are [assuming](http://stackoverflow.com/questions/1402005/how-to-check-if-internet-connection-is-present-in-java) [correctly](http://stackoverflow.com/questions/1139547/detect-internet-connection-using-java) about "thousands of developers before me have tackled this". – Oleg Estekhin May 21 '14 at 08:30
  • @Luaan: you are right, but I guess there's some header element I can set on the HTTP HEAD request which makes sure that any in-between proxy will not try to serve it from its own cache? – peterh May 21 '14 at 09:08
  • @nolan6000 Yeah, there's plenty of ways. In the end, though, the proxy / router can choose to completely ignore it, and I've seen that happen many, many times :D – Luaan May 21 '14 at 09:18

1 Answers1

2

I would recommend you to have a look at this host list. Pick a couple of hosts which seem reasonable to you, and do something like this:

// create a client
var httpClient = new HttpClient();
// create a request to your site / host
var request = new HttpRequestMessage(HttpMethod.Head, new Uri(your-host));
// await it
var response = await httpClient.SendAsync(request);
// Check that the response seems valid, so that it is not just a redirect somewhere else

And catch any exceptions thrown.

A note though, it is very hard to discern whether or not you have actual internet access, since there is no such thing as "total" internet access. You have to define what you mean with internet access :)

flindeberg
  • 4,887
  • 1
  • 24
  • 37