JAVA-企业微信报警工具类

企业微信报警工具类,可用于项目内告警信息通知。

import com.alibaba.fastjson2.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

/**
 * 企业微信报警工具类
 * 文档地址:https://developer.work.weixin.qq.com/document/path/91770
 *
 * @author hou.xy
 */
public class WxWorkAlarmUtil {

    private static final Logger logger = LoggerFactory.getLogger(WxWorkAlarmUtil.class);

    /**
     * 发送http请求
     *
     * @param reqMsg   请求报文
     * @param robotUrl 机器人地址(企业微信群右上角更多菜单中添加群机器人所生成的地址)
     * @param isProxy  是否开启代理
     * @param ip       代理IP
     * @param port     代理端口
     * @throws Exception
     */
    public static void postAlarm(String reqMsg, String robotUrl, boolean isProxy, String ip, Integer port) throws Exception {
        logger.info("发送企业微信请求报文:" + reqMsg);
        StringBuilder resMsg = new StringBuilder();
        HttpsURLConnection httpsUrlConnection = null;
        OutputStream outStr = null;
        BufferedReader in = null;
        SSLSocketFactory ssf = BZX509TrustManager.getSSFactory();
        try {
            URL url = new URL(robotUrl);
            //代理模式
            if (isProxy) {
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));
                httpsUrlConnection = (HttpsURLConnection) url.openConnection(proxy);
            } else {//非代理模式
                httpsUrlConnection = (HttpsURLConnection) url.openConnection();
            }
            httpsUrlConnection.setSSLSocketFactory(ssf);
            httpsUrlConnection.setRequestProperty("accept", "*/*");
            httpsUrlConnection.setRequestProperty("connection", "Keep-Alive");
            httpsUrlConnection.setRequestProperty("Content-type", "application/json");
            httpsUrlConnection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            httpsUrlConnection.setDoOutput(true);
            httpsUrlConnection.setDoInput(true);
            httpsUrlConnection.setUseCaches(false);
            httpsUrlConnection.setRequestMethod("POST");
            httpsUrlConnection.setConnectTimeout(60000);
            httpsUrlConnection.connect();

            //发送请求
            outStr = httpsUrlConnection.getOutputStream();
            HashMap<String, Object> msgHead = new HashMap(16);
            msgHead.put("msgtype", "markdown");

            HashMap<String, Object> msg = new HashMap(16);
            msg.put("content", reqMsg);
            msgHead.put("markdown", msg);

            outStr.write(JSON.toJSONString(msgHead).getBytes(StandardCharsets.UTF_8));
            outStr.flush();
            outStr.close();

            in = new BufferedReader(new InputStreamReader(httpsUrlConnection.getInputStream(), StandardCharsets.UTF_8));
            String line;
            while ((line = in.readLine()) != null) {
                resMsg.append(line);
            }
            logger.info("接收企业微信应答报文:" + resMsg);
        } catch (SocketTimeoutException e) {
            logger.info("", e);
            throw new Exception("读取回执超时", e);
        } catch (ConnectException ce) {
            logger.info("", ce);
            throw new Exception("连接失败 请检查http服务端是否开启", ce);
        } catch (Exception e) {
            logger.info("", e);
            throw new Exception("已连接到服务器,但接收应答出现异常", e);
        } finally {
            //关闭流及连接
            if (outStr != null) {
                outStr.close();
            }
            if (in != null) {
                in.close();
            }
            if (httpsUrlConnection != null) {
                httpsUrlConnection.disconnect();
            }
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值