zoukankan      html  css  js  c++  java
  • jsoup 模拟登陆github网页(源代码)亲测可用 直接复制就能用

    参考博文:https://blog.csdn.net/dietime1943/article/details/73294442

    全部代码:

      1 package Pa;
      2 import java.util.HashMap;
      3 import java.util.List;
      4 import java.util.Map;
      5 
      6 import org.jsoup.Connection;
      7 import org.jsoup.Jsoup;
      8 import org.jsoup.Connection.Method;
      9 import org.jsoup.Connection.Response;
     10 import org.jsoup.nodes.Document;
     11 import org.jsoup.nodes.Element;
     12 
     13 import page.SslUtils;
     14 
     15 /**
     16 
     17  * @since      crawler(datasnatch) version(1.0)</br>
     18 
     19  * @author     bluetata / sekito.lv@gmail.com</br>
     20 
     21  * @reference  http://bluetata.blog.csdn.net/</br>
     22 
     23  * @version    1.0</br>
     24 
     25  * @update     03/14/2019 16:00
     26 
     27  */
     28 
     29 public class jsoup模拟登陆 {
     30 
     31     public static String LOGIN_URL = "https://github.com/login";
     32     public static String USER_AGENT = "User-Agent";
     33     public static String USER_AGENT_VALUE = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0";
     34 
     35     public static void main(String[] args) throws Exception {
     36          try {
     37                 SslUtils.ignoreSsl();
     38             } catch (Exception e) {
     39                 e.printStackTrace();
     40             }
     41         simulateLogin("你的用户名", "你的密码"); // 模拟登陆github的用户名和密码
     42     }
     43   /**
     44 
     45      * @param userName 用户名
     46 
     47      * @param pwd 密码
     48 
     49      * @throws Exception
     50 
     51      */
     52 
     53     public static void simulateLogin(String userName, String pwd) throws Exception {
     54         /* 
     55          * 第一次请求 
     56          * grab login form page first
     57          * 获取登陆提交的表单信息,及修改其提交data数据(login,password)
     58          */
     59         // get the response, which we will post to the action URL(rs.cookies())
     60         Connection con = Jsoup.connect(LOGIN_URL);  // 获取connection
     61         con.header(USER_AGENT, USER_AGENT_VALUE);   // 配置模拟浏览器
     62         Response rs = con.execute();                // 获取响应
     63         Document d1 = Jsoup.parse(rs.body());       // 转换为Dom树
     64         System.out.println(d1);
     65 
     66         List<Element> eleList = d1.select("form");  // 获取提交form表单,可以通过查看页面源码代码得知
     67 
     68         // 获取cooking和表单属性
     69         // lets make data map containing all the parameters and its values found in the form
     70         Map<String, String> datas = new HashMap<>();
     71 
     72         // 01/24/2019 17:45 bluetata 更新 -------------------------------------------------------------- Start ----------
     73 
     74         // GitHub多次改版更新,最新的提交request data为:
     75         // authenticity_token   ll0RJnG1f9XDAaN1DxnyTDzCs+YXweEZWel9kGkq8TvXH83HjCwPG048sJ/VVjDA94YmbF0qvUgcJx8/IKlP8Q==
     76         // commit  Sign+in
     77         // login   bluetata
     78         // password    password123
     79         // utf8    ✓
     80 
     81         for(int i = 0; i < eleList.size(); i++) {
     82 
     83             for (Element e : eleList.get(i).getAllElements()) {
     84                 // 设置用户名
     85                 if (e.attr("name").equals("login")) {
     86                     e.attr("value", userName);
     87                 }
     88 
     89                 // 设置用户密码
     90 
     91                 if (e.attr("name").equals("password")) {
     92                     e.attr("value", pwd);
     93                 }
     94 
     95                 // 排除空值表单属性
     96                 if (e.attr("name").length() > 0) {
     97                     datas.put(e.attr("name"), e.attr("value"));
     98                 }
     99             }
    100         }
    101 
    102 
    103 //      旧逻辑  delete  01/24/2019 17:49 bluetata --------------------------------------------start
    104 //        for (Element e : eleList.get(0).getAllElements()) {
    105 //            // 设置用户名
    106 //            if (e.attr("name").equals("login")) {
    107 //                e.attr("value", userName);
    108 //            }
    109 //            // 设置用户密码
    110 //            if (e.attr("name").equals("password")) {
    111 //                e.attr("value", pwd);
    112 //            }
    113 //            // 排除空值表单属性
    114 //            if (e.attr("name").length() > 0) {
    115 //                datas.put(e.attr("name"), e.attr("value"));
    116 //            }
    117 //        }
    118 //      旧逻辑  delete  01/24/2019 17:49 bluetata --------------------------------------------end
    119         // 01/24/2019 17:45 bluetata 更新 --------------------------------------------------------------- End -----------
    120         /*
    121          * 第二次请求,以post方式提交表单数据以及cookie信息
    122          */
    123 
    124         Connection con2 = Jsoup.connect("https://github.com/session");
    125         con2.header(USER_AGENT, USER_AGENT_VALUE);
    126         // 设置cookie和post上面的map数据
    127         Response login = con2.ignoreContentType(true).followRedirects(true).method(Method.POST).data(datas).cookies(rs.cookies()).execute();
    128         // 打印,登陆成功后的信息
    129         System.out.println(login.body());
    130         // 登陆成功后的cookie信息,可以保存到本地,以后登陆时,只需一次登陆即可
    131         Map<String, String> map = login.cookies();
    132         for (String s : map.keySet()) {
    133             System.out.println(s + " : " + map.get(s));
    134         }
    135 
    136     }
    137 
    138  
    139 
    140 }
  • 相关阅读:
    Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.
    旋转二维数组
    replace empty char with new string,unsafe method和native implementation的性能比较
    判断一字符串是否可以另一字符串重新排列而成
    移除重复字符的几个算法简单比较
    也来纠结一下字符串翻转
    判断重复字符存在:更有意义一点
    程序员常去网站汇总
    sublime
    针对程序集 'SqlServerTime' 的 ALTER ASSEMBLY 失败,因为程序集 'SqlServerTime' 未获授权(PERMISSION_SET = EXTERNAL_ACCESS)
  • 原文地址:https://www.cnblogs.com/smartisn/p/12264584.html
Copyright © 2011-2022 走看看