Jsoup:登录没有提交按钮的ID或名称

时间:2016-04-23 14:35:21

标签: login jsoup

我曾尝试使用Jsoup登录网站,但遗憾的是我已经遇到了一些问题 - 我不知道如何通过提交按钮进入,因为没有ID或者它的名字。你能看看我的代码应该怎么样?

<form action="http://www.abcde.com/index.php?app=core&amp;module=global&amp;section=login&amp;do=process" method="post" id="login">
<input type="hidden" name="auth_key" value="auth_key">
<input type="hidden" name="referer" value="http://www.abcde.com/">
<h3>Login</h3>

<div class="ipsForm ipsForm_horizontal">
    <fieldset>
        <ul>

            <li class="ipsField ipsField_primary">
                <label for="ips_username" class="ipsField_title">Username</label>
                <div class="ipsField_content">
                    <input id="ips_username" type="text" class="input_text" name="ips_username" size="30" tabindex="0">
                </div>
            </li>
            <li class="ipsField ipsField_primary">
                <label for="ips_password" class="ipsField_title">Password</label>
                <div class="ipsField_content">
                    <input id="ips_password" type="password" class="input_text" name="ips_password" size="30" tabindex="0"><br>
                </div>
            </li>

        </ul>
    </fieldset>

    <div class="ipsForm_submit ipsForm_center">
        <input type="submit" class="ipsButton" value="Login" tabindex="0">
    </div>
</div>
</form>

我已经开始了:

 Connection.Respose loginForm = Jsoup.connect("http://www.abcde.com/").method(Connection.Method.GET)
                        .execute();
    Document document = Jsoup
                        .connect("http://www.abcde.com/)
                        .data("cookieexists", "false")
                        .data("ips_username", "username", "ips_password",
                                "password").cookies(loginForm.cookies()).post();

1 个答案:

答案 0 :(得分:0)

首先,您必须向服务器发送GET请求 -

Document doc1 = Jsoup.connect("www.forumowisko.pl")
                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0")
                .get();

然后提取auth_key值 -

Element e = doc.select("input[id=auth_key]").first();
String authKey = e.attr("value"); 

现在您可以发送POST请求 -

Document doc2 = Jsoup.connect("http://www.forumowisko.pl/index.php?app=core&module=global&section=login&do=process")
        .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0")         
        .data("auth_key", authKey)      
        .data("ips_username", "MyUsername")
        .data("ips_password, "MyPassword")
        .data("rememberMe", "1")
        .data("referer", "http://www.forumowisko.pl/")
        .cookies(doc1.cookies())
        .post();

请注意,POST请求与GET请求的网址不同。