1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class DingDingMessageUtil { public static String access_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
public static void sendTextMessage(String msg) { try { Message message = new Message(); message.setMsgtype("text"); message.setText(new MessageInfo(msg)); URL url = new URL("https://oapi.dingtalk.com/robot/send?access_token=" + access_token); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Content-Type", "application/Json; charset=UTF-8"); conn.connect(); OutputStream out = conn.getOutputStream(); ObjectMapper objectMapper = new ObjectMapper(); String textMessage = objectMapper.writeValueAsString(message); byte[] data = textMessage.getBytes(); out.write(data); out.flush(); out.close(); InputStream in = conn.getInputStream(); byte[] data1 = new byte[in.available()]; in.read(data1); System.out.println(new String(data1)); } catch (Exception e) { e.printStackTrace(); } } }
|