简体   繁体   中英

Log in website using Jsoup - Java

I'm trying to log in using linkedin Jsoup , I've tried several ways but always run into the error 403, where I am going wrong?

Test One

 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();

Erro Console

org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403, URL= https://www.linkedin.com at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:590) at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:540) at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:227) at 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>

登录

I decided this way , much faster , with a little more code, but well Assertive

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();
         }

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