java实现post签到

自动定时post

  • 需求分析
  • 第一步:fiddler抓包
  • 第二步:编写java提交post
  • 第三步:部署到服务器上定时运行
    • ①确认Linux环境。
    • ②上传所需jar和写好的Java文件到Linux
    • ③操作权限解决:
    • ④Linux下带jar包编译与运行Java程序
    • ⑤写sh脚本并且配置定时运行
  • 结语

需求分析

  1. 需求:实现发请求、签到自动化,每天定时提交,邮件提示结果,告别手动。(使用应当合理合法!)
  2. 案板上有:1台vps,linux centos7+fiddler抓包工具+jar包。
  3. 实现:先用fiddler抓包工具抓取提交打卡是的HTTP请求,再分析每天不同的地方,仿照着利用Java编写脚本发送类似请求,安排到服务器上设置一个定时任务就完事了。(以前做过用python+appuim现在想想真的憨憨。。)

第一步:fiddler抓包

注:涉及隐私部分用xxxxxx替代

POST https:xxxxxxxxxxxx HTTP/1.1
Host: xxxxxxxxxxx
Connection: keep-alive
Content-Length: 1267
Accept: */*
Origin: xxxxxxxxxxx
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Linux; Android 9; MI 6X Build/PKQ1.180904.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045409 Mobile Safari/537.36 MMWEBID/5700 MicroMessenger/7.0.18.1740(0x2700123B) Process/tools WeChat/arm64 NetType/WIFI Language/zh_CN ABI/arm64
Sec-Fetch-Mode: cors
Content-Type: application/json
Sec-Fetch-Site: same-origin
Referer: xxxxxxxxxxx
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7


{"main":[{"TableName":"T_YQFKTJ","Data":[{"GUID":"xxxxxxxxxxx","TBRQ":"2020-11-01","TXJTSJ":"2020-11-01 00:07:26","XH":"xxxxxxxxxxx","XM":"xxx","SZXY":"xxxxx","BKYJS":"xxxxx","SZX":"","BKBJ":"xxxxx","YJSBJ":"","SS":"xxxxx","HQCS":"xxxxx","TBWD":"","KQWD":"xxxxxx","JYDD":"","SFWQZBLYYZDWQZ":"否","MQSTQK":"","SFWYSBLYYZDWYS":"否","YSJYDD":"","SFWJZGL":"未曾隔离","JZGLKSSJ":"","JZGLJSSJ":"","TNSFYQZBRYGMQJC":"否","QZBRJCDDSSX":"","TNSFZHBZZHTJHBYHB":"否","FHYY2":"","SFJRDH":"否","QTXCGSJ":"","SFDHZQTXQ":"否","JJTZYQGLSJ":"","SFWJJGL":"无","TNSFDDLKHZTJWZWLHYSD":"否","FHYY1":"","LQHZJKMQK":"已领取“绿码“","JSSTSFYJWRYJC":"否","JZGLDD":"","YSMQSTQK":"未缺课","JZBHBMTZGL":"未离校","JJGLYY":"未离杭","JZLXDH":"","JJGLKSSJ":"","JJGLJSSJ":"","JJLXDH":"","QZBRJCSJ":"","QZBRJCFS":"","DDHBRQ":"","LKHZTJHBRQ":"","JCRQ":"","DDWZRQ":"","ZZHZTJWZRQ":"","DDWLHZHYRQ":"","ZZHZTJWLRQ":"","FHSJ2":"","RZYHJCYSQTXZYSJJCYSSJ":"","RZYYSJC":""}],"TableId":"xxxxxxx"}],"sub":[]}

可以看到打卡提交了一个带着json的post请求,接下来需要编写Java代码实现模拟post提交json数据。

第二步:编写java提交post

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import com.alibaba.fastjson.JSONObject;
 
public class testdaka {
 

    private static final String USER = "私人xxxx"; // 发件人称号,同邮箱地址
    private static final String PASSWORD = "xxxxxx"; // 如果是qq邮箱可以使户端授权码,或者登录密码
    public static String mailmessage="";
    public static String searchmessage="";

    public static void search(String path,String data) {
        try {
            URL url = new URL(path);
   
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            PrintWriter out = null;

            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Host", "按需自查xxxxxx");
            conn.setRequestProperty("Connection", "keep-alive");
            conn.setRequestProperty("Accept", "*/*");
            conn.setRequestProperty("Origin", "xxxxx");
            conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; Android 9; MI 6X Build/PKQ1.180904.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045409 Mobile Safari/537.36 MMWEBID/5700 MicroMessenger/7.0.18.1740(0x2700123B) Process/tools WeChat/arm64 NetType/WIFI Language/zh_CN ABI/arm64");
            conn.setRequestProperty("Sec-Fetch-Mode", "cors");
            conn.setRequestProperty("Sec-Fetch-Site", "same-origin");
            conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
            conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7");


            conn.setDoOutput(true);
            conn.setDoInput(true);
            
            conn.setRequestMethod("POST");
            
            out = new PrintWriter(conn.getOutputStream()); 
            
            out.print(data);
            
            out.flush();
                      
            
            
      
            InputStream is = conn.getInputStream();
            
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String str = "";

            

            while ((str = br.readLine()) != null) {
                str=new String(str.getBytes(),"UTF-8");

                searchmessage=str;

                //System.out.println(str);
            }

          

            is.close();

            conn.disconnect();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    public static void send(String path,JSONObject data) {
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            PrintWriter out = null;

            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            conn.setRequestProperty("Host", "xxxxx");
            conn.setRequestProperty("Connection", "keep-alive");
            conn.setRequestProperty("Accept", "*/*");
            conn.setRequestProperty("Origin", "xxxxx");
            conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; Android 9; MI 6X Build/PKQ1.180904.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/77.0.3865.120 MQQBrowser/6.2 TBS/045409 Mobile Safari/537.36 MMWEBID/5700 MicroMessenger/7.0.18.1740(0x2700123B) Process/tools WeChat/arm64 NetType/WIFI Language/zh_CN ABI/arm64");
            conn.setRequestProperty("Sec-Fetch-Mode", "cors");
            conn.setRequestProperty("Sec-Fetch-Site", "same-origin");
            conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
            conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7");

            conn.setDoOutput(true);
            conn.setDoInput(true);
            
            conn.setRequestMethod("POST");

            out = new PrintWriter(conn.getOutputStream()); 
            
            out.print(data.toJSONString());
            
            out.flush();
            
            InputStream is = conn.getInputStream();
            
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String str = "";
            while ((str = br.readLine()) != null) {
            	str=new String(str.getBytes(),"UTF-8");


                mailmessage=str;

            }
            is.close();
            conn.disconnect();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
	//百度的邮件工具类,用到activation-1.1.jar和javax.mail-1.6.0.jar
    public static boolean sendMail(String to, String text, String title){
        try {
            final Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            // props.put("mail.smtp.host", "smtp.qq.com");
            props.put("mail.smtp.host", "smtp.163.com");

            // 发件人的账号
            props.put("mail.user", USER);
            //发件人的密码
            props.put("mail.password", PASSWORD);

            // 构建授权信息,用于进行SMTP进行身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    String userName = props.getProperty("mail.user");
                    String password = props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };
            // 使用环境属性和授权信息,创建邮件会话
            Session mailSession = Session.getInstance(props, authenticator);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 设置发件人
            String username = props.getProperty("mail.user");
            InternetAddress form = new InternetAddress(username);
            message.setFrom(form);

            // 设置收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            // 设置邮件标题
            message.setSubject(title);

            // 设置邮件的内容体
            message.setContent(text, "text/html;charset=UTF-8");
            // 发送邮件
            Transport.send(message);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }


    public static void main(String[] args) {




        //先打卡
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat.format(date);
        //System.out.println(format);
        //每日必改"TBRQ":"2020-11-01","TXJTSJ":"2020-11-01 00:07:26",
        // String TBRQ = "2020-11-01";
         String TBRQ = format;
         String TXJTSJ = format+" 00:08:26";


        String GUID = "xxxxx";
        String XH="xxxxx";
        String XM= "xxxxx";
        String BKBJ = "xxxxxx";
        String LoginUserID ="xxxxxxx";

        String strjson = "{\"main\":[{\"TableName\":\"T_YQFKTJ\",\"Data\":[{\"GUID\":\""+GUID+"\",\"TBRQ\":\""+TBRQ+"\",\"TXJTSJ\":\""+TXJTSJ+"\",\"XH\":\""+XH+"\",\"XM\":\""+XM+"\",\"SZXY\":\"xxxxxxx\",\"BKYJS\":\"xxxx\",\"SZX\":\"\",\"BKBJ\":\""+BKBJ+"\",\"YJSBJ\":\"\",\"SS\":\"xxxxx\",\"HQCS\":\"xxxxxx\",\"TBWD\":\"\",\"KQWD\":\"xxxxxx\",\"JYDD\":\"\",\"SFWQZBLYYZDWQZ\":\"否\",\"MQSTQK\":\"\",\"SFWYSBLYYZDWYS\":\"否\",\"YSJYDD\":\"\",\"SFWJZGL\":\"未曾隔离\",\"JZGLKSSJ\":\"\",\"JZGLJSSJ\":\"\",\"TNSFYQZBRYGMQJC\":\"否\",\"QZBRJCDDSSX\":\"\",\"TNSFZHBZZHTJHBYHB\":\"否\",\"FHYY2\":\"\",\"SFJRDH\":\"否\",\"QTXCGSJ\":\"\",\"SFDHZQTXQ\":\"否\",\"JJTZYQGLSJ\":\"\",\"SFWJJGL\":\"无\",\"TNSFDDLKHZTJWZWLHYSD\":\"否\",\"FHYY1\":\"\",\"LQHZJKMQK\":\"已领取“绿码“\",\"JSSTSFYJWRYJC\":\"否\",\"JZGLDD\":\"\",\"YSMQSTQK\":\"未缺课\",\"JZBHBMTZGL\":\"未离校\",\"JJGLYY\":\"未离杭\",\"JZLXDH\":\"\",\"JJGLKSSJ\":\"\",\"JJGLJSSJ\":\"\",\"JJLXDH\":\"\",\"QZBRJCSJ\":\"\",\"QZBRJCFS\":\"\",\"DDHBRQ\":\"\",\"LKHZTJHBRQ\":\"\",\"JCRQ\":\"\",\"DDWZRQ\":\"\",\"ZZHZTJWZRQ\":\"\",\"DDWLHZHYRQ\":\"\",\"ZZHZTJWLRQ\":\"\",\"FHSJ2\":\"\",\"RZYHJCYSQTXZYSJJCYSSJ\":\"\",\"RZYYSJC\":\"\"}],\"TableId\":\"xxxxxx\"}],\"sub\":[],\"LoginUserID\":\""+LoginUserID+"\",\"GUID\":\""+GUID+"\",\"FORMID\":\"xxxxxxxxxxx\"}";

        //用到了fastjson-1.2.74.jar
        JSONObject jsonobj = JSONObject.parseObject(strjson);
        try {
            send("抓包到的网址",jsonobj);
        } catch (Exception e) {//防止偶然的连接reset
            send("抓包到的网址",jsonobj);
        }
        //再判断

        try {
            search("抓包到的网址,查询可能带日期自行更改");
        } catch (Exception e) {
            search("抓包到的网址,查询可能带日期自行更改");
        }

        //发邮件
        String sendmail ="打卡的结果:"+ mailmessage+"\r\n"+"打卡后确认的结果:"+searchmessage;
        //System.out.println(sendmail);

        sendMail("[email protected]", sendmail, "打卡结果!");
    }
 
}

第三步:部署到服务器上定时运行

①确认Linux环境。

确保系统时间为北京时间。
确保java环境配置完毕。
确保xshell连接正常。

②上传所需jar和写好的Java文件到Linux

用到的jar包有

	activation-1.1.jar,
	fastjson-1.2.74.jar,
	javax.mail-1.6.0.jar,

以及写好的程序daka.java

在xshell连接下用rz命令上传到目录

	/root/mylearn/webapp/daka

③操作权限解决:

修改使用文件夹的操作权限

	sudo chmod -R 777 /root/mylearn/webapp/daka

④Linux下带jar包编译与运行Java程序

jar包之间用:隔开,最后也要加。

javac -cp /root/mylearn/webapp/daka/libs/activation-1.1.jar:/root/mylearn/webapp/daka/libs/fastjson-1.2.74.jar:/root/mylearn/webapp/daka/libs/javax.mail-1.6.0.jar: /root/mylearn/webapp/daka/daka.java

得到class文件后,运行如下。

java -cp /root/mylearn/webapp/daka/libs/activation-1.1.jar:/root/mylearn/webapp/daka/libs/fastjson-1.2.74.jar:/root/mylearn/webapp/daka/libs/javax.mail-1.6.0.jar: daka

⑤写sh脚本并且配置定时运行

写sh脚本

vi daka.sh

按i进入编辑,粘贴如下,:x保存退出。

#!/bin/bash
cd /root/mylearn/webapp/daka
java -cp /root/mylearn/webapp/daka/libs/activation-1.1.jar:/root/mylearn/webapp/daka/libs/fastjson-1.2.74.jar:/root/mylearn/webapp/daka/libs/javax.mail-1.6.0.jar: daka

配置定时

vi /etc/crontab

#改类似定时,这里设置00:10分打卡
 10 00 * * * root /root/mylearn/webapp/daka/daka.sh

装载

 crontab /etc/crontab

重启定时服务

systemctl enable crond.service #开启启动
systemctl restart crond.service #重启服务

结语

前几个月暑假无聊用python+appuim弄过一个自己打卡的脚本,就是滚屏幕模拟点击的那种。想把它部署到服务器,发现不现实,后来想到他打卡不就是个提交表单嘛,不就是个post嘛。所以搞了个这个,以后不用自己打卡了嘿嘿嘿!

你可能感兴趣的:(java,linux,https,post,fiddler)