How to check for unrestricted Internet access? (captive portal detection)

前端 未结 6 986
时光说笑
时光说笑 2020-12-04 09:13

I need to reliably detect if a device has full internet access, i.e. that the user is not confined to a captive portal (also called walled garden), i.e. a

6条回答
  •  渐次进展
    2020-12-04 10:12

    I believe preventing redirection for your connection will work.

    URL url = new URL(hostUrl));
    HttpURLConnection httpConn = (HttpURLConnection)url.openConnection();
    
    /* This line prevents redirects */
    httpConn.setInstanceFollowRedirects( false );
    
    httpConn.setAllowUserInteraction( false );
    httpConn.setRequestMethod( "GET" );
    httpConn.connect();
    responseCode = httpConn.getResponseCode();
    isConnected = responseCode == HttpURLConnection.HTTP_OK;
    

    If that doesn't work, then I think the only way to do it is to check the body of the response.

提交回复
热议问题