Jsoup 模拟登陆

楔子

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

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

测试结果

访问不带cookie的效果
Jsoup 模拟登陆_第1张图片

访问带cookie的效果
Jsoup 模拟登陆_第2张图片

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


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/

你可能感兴趣的:(jsoup,java)