使用Jsoup进行网站登录

时间:2015-04-02 05:41:32

标签: android html parsing xhtml jsoup

我正在尝试在我的Android应用程序上使用Jsoup登录网站,但我无法登录。我已经尝试了几种不同的方法,我认为我找到了最好的方法,但我需要一些帮助来调整它,因为它仍然无法正常工作。以下是网站登录html:

<div id="login-content">
    <form action="" method="post">
        <div class="notification information png_bg">
            <div>
                Please enter your username and password.
            </div>
        </div>
        <p>
            <label>Username</label>
            <input name="username" class="text-input" type="text" />
        </p>
        <div class="clear"></div>
        <p>
            <label>Password</label>
            <input name="password" class="text-input" type="password" />
        </p>
        <div class="clear"></div>
        <!--<p id="remember-password">
                <input type="checkbox" />Remember me
            </p> -->
        <div class="clear"></div>
        <p>
            <input class="button" name="submit" type="submit" value="Sign In" />
        </p>
    </form>
</div> <!-- End #login-content -->

然后这是我用来尝试登录的代码:

private class Login extends AsyncTask<Void, Void, Void> {
    String username;
    String password;


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(HTML.this);
        mProgressDialog.setTitle("Logging in");
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {

        try {

            Connection.Response res = Jsoup.connect(url)
                    .data("username", username, "password", password, "submit", "Sign In", "action", "do_login")
                    .followRedirects(true)
                    .method(Connection.Method.POST)
                    .execute();

        } catch(Throwable t) {
            t.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        mProgressDialog.dismiss();
    }
}

0 个答案:

没有答案