Login to the Wifi network through the app

Asked

Viewed 3,270 times

0

When you connect to a wifi network that needs authentication, Android sends you to the network authentication page by a redirect usually sent by the link connectivitycheck.gstatic.com/generate_204 or by the link clients3.google.com/generate_204 or similar links.

In the app I’m developing, I want to do it automatically through the app. I want him to get the redirected address and log in to the network using this address.

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

Also, how can I check, at runtime, if the user is logged in and with wifi network access?

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

My application has saved the login and password of the user. Thus, it opens internally the wifi login page and login with the saved information. The problem is precisely having known the url.

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

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

1 answer

3


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

  1. make any http request just to drop into the login screen and pick up the address (I used org.apache.http.client.methods.Httpget and org.apache.http.client.methods.Httppost)
  2. look for the url q the "login button" points (if you already know, you don’t need to do this)
  3. send login data (in the same way/same parameters as the form does on the page, probably by POST)
  4. treat the return (in case of a cookie, or token, store/use it correctly for next requests)

Follow an example (already knew the URL of the POST to log in):

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 am doing a POST for the url that the login button on the page points. In this POST, I am sending the username and password. After the POST, I check the response and see in the Answer header if you have a redirect to the login page, if you have, there was no login, if not, there was.

  • I’ll describe the situation better in context. My application has saved the user login and password. Thus, it opens the wifi login page internally and logs in with the saved information, in a way really very similar to what you presented in your code. The problem is just having known the url. This problem happens because it is never the same url. It changes with each login attempt, due to the different token that is generated with each 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.

  • I believe that if you make an http request to any address [q requires you to be logged in before], in the header or in the body of Sponse (I don’t remember now) the login URL will come (because that’s how the browser finds out which URL to redirect you to). Take a good look at Response, especially its header. So you can get the URL, save it in a variable and make a new http request for it.

  • Could you make an example code of how to do this? Just a rough draft would help

  • The example from above does not help?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.