使用Jsoup - Java登录网站

时间:2016-07-13 14:10:02

标签: java forms login jsoup

我正在尝试使用linkedin Jsoup登录,我尝试了几种方法,但总是遇到错误403,我出错了?

测试一次

 Connection.Response loginForm = Jsoup.connect("https://www.linkedin.com")
                        .method(Connection.Method.GET)
                        .execute();

                Document document = Jsoup.connect("https://www.linkedin.com")
                        .data("login-email", "email")
                        .data("login-password", "password")
                        .cookies(loginForm.cookies())
                        .post();

                System.out.println(document.body().html());

//after you logged in go to this page
 Document pesquisa=Jsoup.connect("https://www.linkedin.com/vsearch/p?locationType=Y&f_N=S%2CO&f_G=br%3A6368&f_I=137&trk=vsrp_savedsearch").get();

错误控制台

  

org.jsoup.HttpStatusException:HTTP错误提取URL。状态= 403,   网址= https://www.linkedin.com at   org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:590)     在   org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:540)     在org.jsoup.helper.HttpConnection.execute(HttpConnection.java:227)     在org.jsoup.helper.HttpConnection.post(HttpConnection.java:222)at   linkedin.page.main(page.java:27)

Html Page

<form class="login-form" action="https://www.linkedin.com/uas/login-submit" method="POST" data-autologin="true">
<label for="login-email">E-mail</label>
<input type="text" name="session_key" id="login-email" placeholder="E-mail ou número de telefone" autofocus="autofocus">
<label for="login-password">Senha</label><input type="password" name="session_password" id="login-password" aria-required="true" placeholder="Senha">
<input name="isJsEnabled" type="hidden" value="false">
<input name="loginCsrfParam" id="loginCsrfParam-login" type="hidden" value="1802cd0f-af50-45b8-810c-2ecc3bac3c72">
<input name="trk" id="trk-login" type="hidden" value="nav_responsive_tab_home">
<input name="sourceAlias" id="sourceAlias-login" type="hidden" value="0_7r5yezRXCiA_H0CRD8sf6DhOjTKUNps5xGTqeX8EEoi">
<input type="submit" name="submit" value="Entrar">
</form>

login

1 个答案:

答案 0 :(得分:0)

我决定采用这种方式,速度更快,代码更多,但是很自信

try {

             String url = "https://www.linkedin.com/uas/login?goback=&trk=hb_signin";
             Connection.Response response = Jsoup
                     .connect(url)
                     .method(Connection.Method.GET)
                     .execute();

             Document responseDocument = response.parse();
             Element loginCsrfParam = responseDocument
                     .select("input[name=loginCsrfParam]")
                     .first();

             response = Jsoup.connect("https://www.linkedin.com/uas/login-submit")
                     .cookies(response.cookies())
                     .data("loginCsrfParam", loginCsrfParam.attr("value"))
                     .data("session_key", "email")
                     .data("session_password", "password")
                     .method(Connection.Method.POST)
                     .followRedirects(true)
                     .execute();

             Document document = response.parse();

             System.out.println("Welcome " 
                     + document.select(".act-set-name-split-link").html());


         } catch (IOException e) {
             e.printStackTrace();
         }