企业微信报警工具类,可用于项目内告警信息通知。
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();
}
}
}
}