title: How to check for unrestricted Internet access? (captive portal detection)
body: <p>I need to reliably detect if a device has full internet access, i.e. that the user is not confined to a <em>captive portal</em> (also called <em>walled garden</em>), i.e. a limited subnet which forces users to submit their credentials on a form in order to get full access. </p>
  
  <p>My app is automating the authentication process, and therefore it is important to know that full internet access is not available before starting the logon activity. </p>
  
  <p>The question is <em>not</em> about how to check that the network interface is up and in a connected state. It is about making sure the device has unrestricted internet access as opposed to a sandboxed intranet segment.</p>
  
  <p>All the approaches I have tried so far are failing, because connecting to any well-known host would not throw an exception but return a valid <code>HTTP 200</code> response code because all requests are routed to the login page.</p>
  
  <p>Here are all the approaches I tried but they all return <code>true</code> instead of <code>false</code> for the reasons explained above:</p>
  
  <p>1:
  </p>
  
  <pre class="lang-java prettyprint-override"><code>InetAddress.getByName(host).isReachable(TIMEOUT_IN_MILLISECONDS);
  isConnected = true; &lt;exception not thrown&gt;
  </code></pre>
  
  <p>2:</p>
  
  <pre class="lang-java prettyprint-override"><code>Socket socket = new Socket();
  SocketAddress sockaddr = new InetSocketAddress(InetAddress.getByName(host), 80);
  socket.connect(sockaddr, pingTimeout);
  isConnected = socket.isConnected();
  </code></pre>
  
  <p>3:</p>
  
  <pre class="lang-java prettyprint-override"><code>URL url = new URL(hostUrl));
  URLConnection urlConn = url.openConnection();
  HttpURLConnection httpConn = (HttpURLConnection) urlConn;
  httpConn.setAllowUserInteraction(false);
  httpConn.setRequestMethod("GET");
  httpConn.connect();
  responseCode = httpConn.getResponseCode();
  isConnected = responseCode == HttpURLConnection.HTTP_OK;
  </code></pre>
  
  <p>So, how do I make sure I connected to an actual host instead of the login redirection page? Obviously, I could check the actual response body from the 'ping' host I use but it does not look like a proper solution.</p>