寝室电费查询预警提示

寝室总是突然断电,不喜欢经常去查看电费,看这篇文章就够了

1.首先学会fidder抓包,抓取你们学校的电费查询接口

电脑上抓包

Fiddler抓包工具之详细使用步骤(超详细)_fiddler使用教程_上网的虫不叫网虫的博客-CSDN博客

手机上抓包

fiddler抓取iOS微信小程序HTTPS包方法-CSDN博客

再下载一下下面这个教程的插件

Fiddler配置抓取https数据包-CSDN博客

2.配置邮箱

配置教程:安卓给qq邮箱发送验证码-CSDN博客

3.代码

package com.example.demo;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


@SpringBootApplication

public class Demo1Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Demo1Application.class, args);
        ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
        // 延时5小时后开始执行,然后每隔5小时执行一次
        scheduler.scheduleAtFixedRate(Demo1Application::executeTask, 5, 5, TimeUnit.HOURS);
//        executeTask();


    }
    private static void executeTask(){

        try {
            // 运行需要定时执行的任务代码逻辑
            // 创建URL对象
            URL url = new URL("抓包下来的接口");

            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法
            connection.setRequestMethod("POST");

            // 添加请求头
            connection.setRequestProperty("Content-Type", "application/json;");
            connection.setRequestProperty("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.42(0x18002a2a) NetType/WIFI Language/zh_CN");

            // 构建请求体
            String requestBody = "填写需要的参数(json格式)";

            // 发送请求体
            connection.setDoOutput(true);
            try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
                byte[] requestBodyBytes = requestBody.getBytes(StandardCharsets.UTF_8);
                outputStream.write(requestBodyBytes, 0, requestBodyBytes.length);
            }

            // 发起请求
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            // 读取响应数据
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuffer response = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            // 输出响应结果
            JSONObject json = new JSONObject(response.toString());
            JSONObject jsonObject = new JSONObject(json.get("resultData").toString());
            System.out.println("Response Body: " + jsonObject.get("odd"));
            float dianfei = Float.parseFloat(jsonObject.get("odd").toString());
            if (dianfei<20){
                SendMail.sendEmail("3225电费警告!","剩余:"+dianfei+"度");
            }

            // 关闭连接
            connection.disconnect();

            System.out.println("主函数定时执行");

        } catch (Exception e) {
            // 处理异常
            e.printStackTrace();
        }


    }
}

package com.example.demo;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class SendMail {
    public static void sendEmail(String Zhuti,String leirong) throws Exception {

        // 配置SMTP服务器和端口
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.qq.com");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");

        // 创建会话
        Session session = Session.getInstance(props);

        // 创建邮件
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("你自己的@qq.com","电费提醒"));
        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("想要提醒的人的@qq.com"));
        message.setSubject(Zhuti);
        message.setText(leirong);

        // 发送邮件
        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.qq.com", "自己@qq.com", "qq邮箱密钥");
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
        System.out.println("邮件发送成功!");
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值