Search code examples
javaauthenticationwimax

How to login a Webpage and retrieve data to a Java Application?


I want to make an app for my own using. To show the wimax signals, CINR, Upload Speed etc. When the device connects, open the browser http://192.168.2.1 , after login (javascript login paage) it shows the values.

I want to get those data to my apps. So, how can I do that? Any example?

Image of Info's


Solution

  • You may be able to use a library like Jsoup to parse the HTML data.

    I'm assuming this is a router you're trying to access, which normally uses basic authentication. You'll also need a library to do base64 encoding for basic authentication, such as Apache commons.

    import org.apache.commons.codec.binary.Base64;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    
    ...
    
    public static void main(String[] args) {
    
        String username = "username";
        String password = "password";
        String login = username + ":" + password;
        String base64login = new String(Base64.encodeBase64(login.getBytes()));
    
        Document document = Jsoup
            .connect("http://192.168.2.1")
            .header("Authorization", "Basic " + base64login)
            .get();
    
        Element e = document.select("body");
        ...
        /* Select the elements and HTML text you're interested here */
    }
    

    That should be enough to get you started. You can learn more about how to use Jsoup here