简体   繁体   中英

How to login by filling a form in a website using Android

I'm trying to log on to a website that has a form to which you should provide user-name and password, check a box, and press a login button. I tried all kinds of httpClient POST messages, but it seems that it is not working. Can anyone assist and point to an example of skeleton of android Java way to login? Here is the form from the html page:

    <form name="loginForm" method="post" action="/login.do">
    <table border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td width="10px">&nbsp;</td>
            <td><label class="formLabel" for="loginID">Username</label></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="text" name="username" value="" class="formTextField"></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><label class="formLabel" for="password"> Password</label></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="password" name="password" value="" class="formTextField"></td>
        </tr>

        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="checkbox" name="agreement" value="on" class="formTextField"> 

                I agree with <div>
                <b><a href="javascript:openPopup('ext/ibsContent/terms.pdf')">Terms and Conditions</a></b></div>

                </td>
        </tr>

    </table>
    <p><input type="submit" value="Login" class="FPFormFieldB"></p>
    <p><a href="navigate.do?anode=user_ChangePassword">Have you forgotten the password?</a></p>
    <p><a href="navigate.do?anode=user_Registration">New user registration</a></p>

    </form>

I managed to login using JSOUP. The key is that you need to do a get first, then a post, with the cokies (that includes SessionID and other stuff).

Here is the code that worked for me, hopefully it will assist others:

import android.provider.DocumentsContract;

import org.jsoup.Jsoup;
import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;


public class Webbing {

public static void Open() throws Exception {

    Connection.Response loginForm = Jsoup.connect("http://your website")
            .method(Connection.Method.GET)
            .execute();

    Document document = Jsoup.connect("http://your website")
            .data("username", "XXX")
            .data("password", "YYY")
            .data("agreement", "on")
            .timeout(5000)
            .cookies(loginForm.cookies())
            .post();

    String url = "http://a page you want to load after login";

    Document fpl = Jsoup.connect(url)
            .timeout(5000)
            .cookies(loginForm.cookies())
            .get();
    body = fpl.body().toString();
    ExtFile.write(body);

}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM