写了一个可以自动获取登录结果并且自动跳转的脚本解放双手

目录

前言

脚本源码

简单分析


小伙伴们大家好,懒惰使人类进步,用代码解放双手的劳动

前言

对于一个系统正式上线前,会分为好几个环境(开发人员专用,测试人员专用,产品专用等),比如我负责的这个项目,就是分了三个环境,每天来到公司第一件事就是登录页面,进去后打开开发者页面,获取到一个登录成功返回的token,然后拿着这个token拼到开发环境的链接后面才能进入开发环境,乐此不疲(bushi)

感觉也还好不怎么麻烦,但是日复一日略显枯燥,还是交给代码吧。。。来看下效果。脚本很简单,新建一个类,执行main方法即可,点击输出的链接即可登录

脚本源码

java 复制代码
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DevLink {
    public static void main(String[] args) throws Exception {

        String path = "C:\\Users\\xxx\\Desktop\\链接.txt";
        List<String> http = new ArrayList();
        try {
            Stream<String> lines = Files.lines(Paths.get(path));
            http = lines.filter(e -> e.contains("http"))
                    .collect(Collectors.toList());
        } catch (IOException e) {
            throw new Exception("读取文件出错" + e.getMessage());
        }

        System.out.println("-----------------------------文件读取成功------------------------------------------");

        String url = "https://cloud-test.clinflash.com/api/users/login";
        URL url1 = new URL(url);
        HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type", "application/json");
        String requestBody = "{\n" +
                "  \"username\": \"xxx@xxx.com\",\n" +
                "  \"password\": \"xxx.xxx\"\n" +
                "}";
        urlConnection.setDoOutput(true);
        urlConnection.getOutputStream().write(requestBody.getBytes("utf-8"));

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        String inputLine;
        //获取请求结果
        StringBuffer stringBuffer = new StringBuffer();
        while ((inputLine = bufferedReader.readLine()) != null) {
            stringBuffer.append(inputLine);
        }
        String needUrl = http.get(3);
        System.out.println(s);
        bufferedReader.close();

        System.out.println("response code : " + urlConnection.getResponseCode());
        System.out.println("response body : " + stringBuffer.toString());
        String result = stringBuffer.toString();
        JsonObject jsonObject = new Gson().fromJson(result, JsonObject.class);
        JsonElement jsonElement = jsonObject.getAsJsonObject("data").get("token");
        String token = "";
        if (jsonElement != null) {
            token = jsonElement.getAsString();
        }
        String usefulConn = needUrl + token;
        System.out.println(usefulConn);
    }
}

简单分析

  • 首先读取存放链接的文件,逐行读取,筛选除以自定义规则开头的链接地址
  • 配置需要登录的链接,配置相关参数,请求头等
  • 接收相应参数以及响应结果,需要的token就在响应结果中
  • 将响应结果转换为字符串,然后调整为json格式
  • 获取json格式中的某个字段的值,然后拼到链接后面即可

右键运行,查看控制台,点击输出结果,成功验证token并跳转到页面

好了 文章到这里就结束了


相关推荐
H H.11 小时前
Java企业级开发:从框架到工程化实战
java·开发语言·经验分享·其他·课程设计
wno70411 小时前
springboot整合FastDFS
java·spring boot·后端
顧棟12 小时前
组件生命周期设计:从硬编码初始化到状态机
java·ide
特立独行的猫A20 小时前
别再用 nohup 了:让应用在后台常驻的正确姿势与 systemd 服务详解
java·后端·python
一起_java20 小时前
技术文章-基于Dify-Agent的智能问答系统设计与实现
java·spring boot·后端·agent·dify
Flynn Liang1 天前
I/O 密集型高并发实战:Java 服务 10 维优化,从线程打满到 10 万 QPS
java·spring boot·性能优化·高并发·系统设计·虚拟线程·io密集型
吃我一窝jio1 天前
Python与Go线程模型深度对比
java·开发语言·python
这个昵称不容易1 天前
SpringBoot 购物推荐网站的设计与实现:120 个 Java 文件、147 个接口、13 张表,附真实运行截图
java·springboot·毕设
文丑颜不良啊1 天前
LeetCode3041~3060题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode3101~3120题目解析
java·leetcode