如何使用JSOUP登录网站?

时间:2015-12-08 08:02:12

标签: android web android-asynctask jsoup

这是我要登录的网站的HTML正文:

<body onload="setFocusOnLoginForm();">
    <div class="login-container">
        <div class="login-header">
            <h2>OSS Аргус</h2>
        </div>
        <div class="login-content">
        <form method="post" action="j_security_check" name="loginform"
            id="loginform" target="_parent" style="padding: 2px;">

        <table align="center" class=loginTable>

                    <tr class="form-field">
                        <td><label for="j_username" style="white-space: nowrap;">Логин</label></td>
                        <td><input type="text" name="j_username" id="j_username" size="12"
                            value=""/>
                        </td>
                    </tr>

                    <tr class="form-field">
                        <td><label for="j_password" style="white-space: nowrap;">Пароль</label></td>
                        <td><input type="password" name="j_password" id="j_password" value="" size="12" /></td>
                    </tr>

                    <tr>
                        <td/>
                        <td><input type="submit" value="LOGIN" class="login-button" /></td>
                    </tr>

        </table>


        </form>
        </div>
    </div>



</body>

以下是我的登录代码:

protected Void doInBackground(Void... params) {

        Document doc = null;
        Document arg = null;
        try {


            disableSSLCertCheck();
            Connection.Response loginForm = Jsoup.connect("https://URL/home.seam")
                    .data("j_username", "USERNAME", "j_password", "PASSWORD", "login-button", "LOGIN")
                    .followRedirects(true)
                    .method(Connection.Method.POST)
                    .execute();
            doc = Jsoup.connect("https://URL/home.seam")
                    .userAgent("Chrome")
                    .cookies(loginForm.cookies())
                    .get();
            /*    doc = Jsoup.connect("https://URL/taskList.seam")
                    .userAgent("Mozilla")
                    .get();*/

        } catch (IOException e) {

            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }

怎么了?我无法登录

我刚读过Logging in to website programatically Android 但在我的网站上不是这样的参数特别是登录按钮。

1 个答案:

答案 0 :(得分:0)

您可能遗漏了一些标题。

尝试使用您喜欢的浏览器登录并嗅探它发送的标题。

获得这些标题的列表后,请在代码中添加它们。