当前位置:   article > 正文

JAVA连接企业微信机器人_java对接企业微信机器人

java对接企业微信机器人
  1. public static void sendMessage(String webhookUrl, String message) throws IOException {
  2. URL url = new URL(webhookUrl);
  3. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  4. connection.setRequestMethod("POST");
  5. connection.setRequestProperty("Content-Type", "application/json");
  6. connection.setDoOutput(true);
  7. String jsonPayload = "{\"msgtype\":\"text\",\"text\":{\"content\":\"" + message + "\"}}";
  8. try (OutputStream outputStream = connection.getOutputStream()) {
  9. byte[] input = jsonPayload.getBytes("utf-8");
  10. outputStream.write(input, 0, input.length);
  11. }
  12. int responseCode = connection.getResponseCode();
  13. if (responseCode != 200) {
  14. throw new IOException("Message sending failed with response code: " + responseCode);
  15. }
  16. }

main函数运行该方法测试

WebhookUrl是机器人的链接

message是要发出的信息

运行main函数 发送成功过 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/178489?site
推荐阅读
相关标签
  

闽ICP备14008679号