依赖jar包:
httpclient-4.5.5.jar
httpcore-4.4.1.jar
httpclient-cache-4.1.3.jar
package base.util; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; /** * @author 马家立 * @version 创建时间:2020年2月4日上午10:27:34 * @Description:TODO 钉钉推送Util */ public class DingDingSendUtil { static Logger forPk = Logger.getLogger("forPk"); //推送到钉钉的department群指令 static String pk_WEBHOOK_TOKEN ="https://oapi.dingtalk.com/robot/send?access_token=略"; /** * @Title:sendDingPkMessage * @author:马家立 * @date:2020年2月4日 上午10:33:24 * @Description:TODO 钉钉推送排课数据 * @param sendContent--待推送的排课数据信息 * @param WEBHOOK_TOKEN--推送到钉钉的群指令 * @return--String */ public static String sendDingPkMessage(String sendContent) { forPk.info("进入DingDingSendBean的sendDingPkMessage"); try { //单独推送人的手机号 String sendPhone = ""; //是否@所有人(true为所有人,false为不@所有人) Boolean isAtAll = false; // 监控数据信息推送到钉钉群根据传入Boolean值判断是否@所有人 String result = sendDingMessage(pk_WEBHOOK_TOKEN, sendContent, sendPhone, isAtAll); return result; } catch (Exception e) { forPk.error("DingDingSendBean的sendDingPkMessage err", e); return "error"; } } /** * @Title:sendDingMessage * @author:马家立 * @date:2020年2月4日 上午10:37:15 * @Description:TODO 监控数据信息推送到钉钉群根据传入Boolean值判断是否@所有人 * @param WEBHOOK_TOKEN--钉钉群机器指令(http格式) * @param sendContent--钉钉群机器指令(http格式) * @param sendPhone--推送手机号 * @param isAtAll--是否@所有人(true为所有人,false为不@所有人) * @return--String */ public static String sendDingMessage(String WEBHOOK_TOKEN, String sendContent, String sendPhone, Boolean isAtAll) { forPk.info("进入DingDingSendBean的sendDingMessage"); try { String result = ""; // new一个post的http (Ps:WEBHOOK_TOKEN是群机器人的推送指令) HttpPost httppost = new HttpPost(WEBHOOK_TOKEN); httppost.addHeader("Content-Type", "application/json; charset=utf-8"); // 钉钉参数格式拼接 String sendMsg = "{ \"msgtype\": \"text\", \"text\": {\"content\": \"" + sendPhone + "" + sendContent + "\"}, \"at\":{\"atMobiles\":[\"" + sendPhone + "\"], \"isAtAll\": " + isAtAll + "}}"; // 设置编码格式utf-8 StringEntity encode = new StringEntity(sendMsg, "utf-8"); httppost.setEntity(encode); // 创建http客户端 HttpClient httpclient = HttpClients.createDefault(); // 响应http客户端 HttpResponse response = httpclient.execute(httppost); forPk.info("response.getStatusLine().getStatusCode()==" + response.getStatusLine().getStatusCode()); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { result = EntityUtils.toString(response.getEntity(), "utf-8"); } else { result = "钉钉信息推送失败"; } forPk.info("钉钉信息发送结果:" + result); return result; } catch (Exception e) { forPk.error("DingDingSendBean的sendDingMessage err", e); return "error"; } } /** * @Title:main * @author:马家立 * @date:2019年1月9日 下午5:49:12 * @Description:main函数测试 * @param:@param args */ public static void main(String[] args) { DingDingSendUtil.sendDingPkMessage("排课测试推送钉钉"); System.out.println("over"); } }