基于java使用釘釘機(jī)器人向釘釘群推送消息
這篇文章主要介紹了基于java使用釘釘機(jī)器人向釘釘群推送消息,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
第一步、登錄釘釘電腦版,獲得釘釘機(jī)器人的webhook;






第二步,用java發(fā)送post請(qǐng)求給釘釘完成消息推送
package com.thinkgem.wlw.modules.lhjh.DingTalk;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
* @Author: zhouhe
* @Date: 2019/6/20 14:49
*/
public class SendHttps {
private static Logger logger = LoggerFactory.getLogger(SendHttps.class);
/**
* 發(fā)送POST請(qǐng)求,參數(shù)是Map, contentType=x-www-form-urlencoded
*
* @param url
* @param mapParam
* @return
*/
public static String sendPostByMap(String url, Map<String, Object> mapParam) {
Map<String, String> headParam = new HashMap();
headParam.put("Content-type", "application/json;charset=UTF-8");
return sendPost(url, mapParam, headParam);
}
/**
* 向指定 URL 發(fā)送POST方法的請(qǐng)求
*
* @param url 發(fā)送請(qǐng)求的 URL
* @param param 請(qǐng)求參數(shù),
* @return 所代表遠(yuǎn)程資源的響應(yīng)結(jié)果
*/
public static String sendPost(String url, Map<String, Object> param, Map<String, String> headParam) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接
URLConnection conn = realUrl.openConnection();
// 設(shè)置通用的請(qǐng)求屬性 請(qǐng)求頭
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Fiddler");
if (headParam != null) {
for (Entry<String, String> entry : headParam.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
}
// 發(fā)送POST請(qǐng)求必須設(shè)置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
// 獲取URLConnection對(duì)象對(duì)應(yīng)的輸出流
out = new PrintWriter(conn.getOutputStream());
// 發(fā)送請(qǐng)求參數(shù)
out.print(JSON.toJSONString(param));
// flush輸出流的緩沖
out.flush();
// 定義BufferedReader輸入流來讀取URL的響應(yīng)
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
logger.info("發(fā)送 POST 請(qǐng)求出現(xiàn)異常!" + e);
e.printStackTrace();
}
//使用finally塊來關(guān)閉輸出流、輸入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}
第三步,編寫測(cè)試類
package com.thinkgem.wlw.modules.lhjh.DingTalk;
import java.util.HashMap;
import java.util.Map;
/**
* @Author: zhouhe
* @Date: 2019/6/20 14:52
*/
public class SendMessage {
public static void main(String[] args){
// 釘釘?shù)膚ebhook
String dingDingToken="https://oapi.dingtalk.com/robot/send?access_token=0f0daca33m98gn78f00189fe1e1e908b81fa26d0d8ddd48fa78a844cd8636187";
// 請(qǐng)求的JSON數(shù)據(jù),這里我用map在工具類里轉(zhuǎn)成json格式
Map<String,Object> json=new HashMap();
Map<String,Object> text=new HashMap();
json.put("msgtype","text");
text.put("content","臨渙焦化:VOCs排放濃度大于上限:61.89");
json.put("text",text);
// 發(fā)送post請(qǐng)求
String response = SendHttps.sendPostByMap(dingDingToken, json);
System.out.println("相應(yīng)結(jié)果:"+response);
}
}
測(cè)試結(jié)果如下:


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何基于SpringMVC實(shí)現(xiàn)斷點(diǎn)續(xù)傳(HTTP)
這篇文章主要介紹了如何基于SpringMVC實(shí)現(xiàn)斷點(diǎn)續(xù)傳(HTTP),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
SpringBoot監(jiān)聽Redis key失效事件的實(shí)現(xiàn)代碼
這篇文章給大家介紹了SpringBoot實(shí)現(xiàn)監(jiān)聽Redis key失效事件的方法,文中通過代碼示例給大家講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2024-02-02
Java8 Stream Collectors收集器使用方法解析
這篇文章主要介紹了Java8 Stream Collectors收集器使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
SpringBoot 動(dòng)態(tài)配置Profile環(huán)境的方式
這篇文章主要介紹了SpringBoot 動(dòng)態(tài)配置Profile環(huán)境的方式,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10
java設(shè)計(jì)模式:建造者模式之生產(chǎn)線
這篇文章主要介紹了Java設(shè)計(jì)模式之建造者模式,結(jié)合具體實(shí)例形式分析了建造者模式的概念、原理、實(shí)現(xiàn)方法與相關(guān)使用注意事項(xiàng),需要的朋友可以參考下2021-08-08

