Jsoup 模拟登陆

10 篇文章 0 订阅

楔子

之前用JSOUP 模拟登陆 签到 获取积分(只需要 用户密码即可登陆的情况)。
好奇 Jsoup 模拟 浏览器访问 ,他是如何保持连接状态的。( 比如 传统 应用依靠session 判断是否登陆)

经测试 Jsoup.execute() 带上cookie就可以 模拟 浏览器保持登陆状态

测试结果

访问不带cookie的效果
在这里插入图片描述

访问带cookie的效果
在这里插入图片描述

模拟浏览器访问的方法如下


import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.util.Map;


public class SignIn {

    private static final String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.34 Safari/537.36 Edg/83.0.478.25";
    // 登录URL|退出RUL
    private static String URL_LOGIN = "http://localhost:9590/thy/jsoup/sign";
    private static String URL_LOGOUT = "http://localhost:9590/thy/jsoup/logout";
    private static final String URL_Sign_in = "http://localhost:9590/thy/jsoup/pri/date";
    // 签到成功后的cookie
    private static Map<String, String> cookies;
    private static Connection connection;

    static {
        System.out.println("---------签到------------");
        connection = Jsoup.connect(URL_LOGIN).userAgent(userAgent)//
                .timeout(10 * 1000).method(Connection.Method.POST)  // // 字段参数| 文件名称| 文件流
                //.header("Connection", "keep-alive")
                .followRedirects(true);
        Connection.Response execute = null;
        try {
            execute = connection.ignoreContentType(true).execute();
            System.out.println(execute.body());
        } catch (IOException e) {
            e.printStackTrace();
        }
        cookies = execute.cookies();
        System.out.println("登录cookie如下");
        System.out.println(cookies);
    }

    /**
     * jsoup 模拟签到
     *
     * @param args
     */
    public static void main(String[] args) throws IOException {
        signForScore(connection, cookies);
        System.out.println("--- 退出登录--------");
        Connection.Response response = connection.url(URL_LOGOUT).method(Connection.Method.GET)//
                .ignoreContentType(true)//
                .followRedirects(true).execute();
        System.out.println(response.body());

    }

    /**
     * 带上cookie可以模拟登陆
     *
     * @param connection
     * @param cookies
     * @throws IOException
     */
    private static void signForScore(Connection connection, Map<String, String> cookies) throws IOException {
        Connection.Response response = connection.url(URL_Sign_in).method(Connection.Method.POST)//
                .cookies(cookies)//带上cookie可以模拟登陆
                .execute();
        System.out.println(response.body());
        Map<String, String> signCookie = response.cookies();
        System.out.println("模拟签到 得到的cookie");
        System.out.println(signCookie);
        System.out.println("------------");
    }
}

测试登陆 退出签到的方法 如下

JsoupController 模拟 controller
LoginInterceptor 模拟拦截器 判断是否登陆
代码码云https://gitee.com/demo51/spthymeleaf/

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值