Log in to the Wifi network through the application

0

When you connect to a Wi-Fi network that requires authentication, Android sends you to the network authentication page by a redirect generally sent by the link connectivitycheck.gstatic.com/generate_204 or by link clients3.google.com/generate_204 or similar links.

In the application I'm developing, I want to do this automatically by the app. I want it to get the redirected address and log in to the network using that address.

How can I get access to these links within the application and get the link to which they redirect, all in the background?

In addition, how can I check to see if the user is logged in and with wifi access?

  

Editing to complement: I will better describe the situation in context.

     

My application has saved user login and password. So it opens   internally the wifi login page and log in with the   saved information. The problem is just having known the url.

     

This problem happens because it is never the same url. It changes every   login attempt, due to the different token that is generated each time   login.

     

My goal is to somehow get this url to send to the   login function, and so know the url to send to the function.

    
asked by anonymous 31.01.2018 / 03:50

1 answer

2

Good morning. I do not understand if automatically connecting to the wifi already opens the browser on the login screen or if the login screen appears when trying to navigate after connecting to the wifi. Home If it is the second option, you can create an android service that is "listening" which wifi network the device has connected. After detecting that there was a connection on that network, you can:

  • Make an http request just to drop in the login screen and get the address (I used org.apache.http.client.methods.HttpGet and org.apache.http.client.methods.HttpPost)
  • search for the url that the "login button" points (if you know, you do not need to do this)
  • send the login data (in the same way / same parameters that the form does on the page, probably by POST)
  • handle the return (in the case of a cookie, or token, store it / use it correctly for next requests)
  • Here is an example (you already knew the POST URL to login):

    logger.info("Logando...");
    
    HttpPost post = getHttpPost(System.getProperty(Propriedades.urlLogin));
    
    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    
    urlParameters.add(new BasicNameValuePair("j_username", System.getProperty(Propriedades.urlUsername)));
    urlParameters.add(new BasicNameValuePair("j_password", System.getProperty(Propriedades.urlSenha)));
    
    post.setEntity(new UrlEncodedFormEntity(urlParameters));
    
    HttpResponse response = client.execute(post);
    
    Header header = response.getFirstHeader("Location");
    if ( header == null || !Strings.nullToEmpty(header.getValue()).contains(urlLogin) )
        throw new LoginSenhaInvalidosException(System.getProperty(Propriedades.urlUsername), System.getProperty(Propriedades.urlSenha));
    
    
    int initIndex = header.getValue().indexOf("jsessionid=");           
    sessao = header.getValue().substring(initIndex, header.getValue().indexOf("?", initIndex));
    
    logger.info("Logon OK");
    

    In this example, I'm doing a POST to the url that the login button on the page points to. In this POST, I am sending the username and password. After the POST, I check the response and see in the header of the response if it has a redirect to the login page, if it has, there was no login, if not, there was.

        
    31.01.2018 / 11:30