Java代码实现向企业微信用户发送消息

java;gutter:true; 1. 其实就是一个HTTP请求,如下 请求方式:POST(HTTPS) 请求地址: https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN</p> <p>文本消息请求参数实例如下</p> <p>{ "touser" : "UserID1|UserID2|UserID3",//用户的ID, "toparty" : "PartyID1|PartyID2",//部门id "totag" : "TagID1 | TagID2",//标签id "msgtype" : "text",//消息类型 "agentid" : 1,//应用的ID,比如时公告还是通知什么一类的,可参考企业微信开发者文档 "text" : { //类型和内容 "content" : "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看邮件中心视频实况,聪明避开排队。" }, "safe":0//是否保密信息 }</p> <p>//其中 touser、toparty、totag不能同时为空 其他如图片类型,语音则可以参考开发者文档中的类型对应设置:企业微信-开发者文档</p> <p>pom依赖</p> <pre><code>com.google.code.gson gson 2.8.2 org.apache.httpcomponents httpclient 4.5.2 org.apache.httpcomponents httpcore 4.4.5 </code></pre> <ol> <li>获取access_token 用到的UrlData类和WeChatData</li> </ol> <p>import org.springframework.stereotype.Component;</p> <p>@Component public class UrlData { String corpId; String corpSecret; String getTokenUrl; String sendMessageUrl;</p> <pre><code>public String getCorpId() { return corpId; } public void setCorpId(String corpId) { this.corpId = corpId; } public String getCorpSecret() { return corpSecret; } public void setCorpSecret(String corpSecret) { this.corpSecret = corpSecret; } public void setGetTokenUrl(String corpid, String corpsecret) { this.getTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret; } public String getGetTokenUrl() { return getTokenUrl; } public String getSendMessageUrl() { sendMessageUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="; return sendMessageUrl; } </code></pre> <p>}</p> <p>package com.lls.it.ldapapi.entity;</p> <p>import org.springframework.stereotype.Component;</p> <p>@Component public class WeChatData { String touser; String msgtype; int agentid; Object text;</p> <pre><code>public Object getText() { return text; } public void setText(Object text) { this.text = text; } public String getMsgtype() { return msgtype; } public void setMsgtype(String msgtype) { this.msgtype = msgtype; } public int getAgentid() { return agentid; } public void setAgentid(int agentid) { this.agentid = agentid; } public String getTouser() { return touser; } public void setTouser(String touser) { this.touser = touser; } </code></pre> <p>} 请求方式:GET(HTTPS) 请求URL:https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRECT 注:此处标注大写的单词ID和SECRET,为需要替换的变量,根据实际获取值更新。</p> <p>public String getToken(String corpId, String corpSecret) throws IOException { SendMsgService sw = new SendMsgService(); UrlData uData = new UrlData(); uData.setGetTokenUrl(corpId, corpSecret); String resp = sw.toAuth(uData.getGetTokenUrl());//就是按照GET方式拼接了字符串得到url Map map = gson.fromJson(resp, new TypeToken>() { }.getType()); System.out.println(map); return map.get("access_token").toString(); }</p> <p>protected String toAuth(String Get_Token_Url) throws IOException {</p> <pre><code> httpClient = HttpClients.createDefault(); httpGet = new HttpGet(Get_Token_Url); CloseableHttpResponse response = httpClient.execute(httpGet); System.out.println(response.toString()); String resp; try { HttpEntity entity = response.getEntity(); System.out.println(response.getAllHeaders()); resp = EntityUtils.toString(entity, "utf-8"); EntityUtils.consume(entity); } finally { response.close(); } return resp; } </code></pre> <ol> <li>POST请求,根据上一步得到的token,发送post请求 应用支持推送文本、图片、视频、文件、图文等类型。</li> </ol> <p>请求方式:POST(HTTPS) 请求地址: https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN</p> <p>参数说明:</p> <p>参数 是否必须 说明 access_token 是 调用接口凭证 请求body就是文章开头的json数据(text类型)</p> <p>代码如下:</p> <p>/*<em> * 创建POST BODY </em>/ private String createPostData(String touser, String msgtype, int agent_id, String contentKey, String contentValue) { WeChatData weChatData = new WeChatData(); weChatData.setTouser(touser); weChatData.setAgentid(agent_id); weChatData.setMsgtype(msgtype); Map content = new HashMap(); content.put(contentKey, contentValue + "\n--------\n" + df.format(new Date())); weChatData.setText(content); System.out.println(gson.toJson(weChatData)); return gson.toJson(weChatData); }</p> <pre><code>/** * POST请求 */ private String post(String charset, String contentType, String url, String data, String token) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); httpPost = new HttpPost(url + token); httpPost.setHeader(CONTENT_TYPE, contentType); httpPost.setEntity(new StringEntity(data, charset)); CloseableHttpResponse response = httpclient.execute(httpPost); String resp; try { HttpEntity entity = response.getEntity(); resp = EntityUtils.toString(entity, charset); EntityUtils.consume(entity); } finally { response.close(); } return resp; } </code></pre> <ol> <li> <p>调用方法请求发送 private CloseableHttpClient httpClient; private HttpPost httpPost;//用于提交登陆数据 private HttpGet httpGet;//用于获得登录后的页面</p> <p>public static final String CONTENT_TYPE = "Content-Type"; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// private static Gson gson = new Gson();</p> </li> </ol> <p>public void sendTextMesg(String toUser, String contentValue) throws IOException { String token = getToken("Your_corpId", "Your_corpSecret"); String postData = createPostData(toUser, "text", 1000002, "content", contentValue); String response = post("utf-8", SendMsgService.CONTENT_TYPE, (new UrlData()).getSendMessageUrl(), postData, token); System.out.println("获取到的token======>" + token); System.out.println("请求数据======>" + postData); System.out.println("发送微信的响应数据======>" + response); }</p> <p>版权声明:本文为CSDN博主「jay_boolean」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_22798455/article/details/81216589

Original: https://www.cnblogs.com/xianz666/p/16053444.html
Author: 红尘沙漏
Title: Java代码实现向企业微信用户发送消息

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/537986/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • SoapUI5.0创建WebService接口模拟服务端(转)

    转载自:https://blog.csdn.net/a19881029/article/details/26348627 使用SoapUI创建WebService接口模拟服务端需要…

    Java 2023年5月30日
    091
  • 虚拟机克隆以后出现“需要整合虚拟机磁盘”的解决方法

    问题描述 在虚拟机克隆完毕以后,原始虚拟机提示”需要整合虚拟机磁盘” 在”任务与事件”栏中看到以下信息 ; 解决方法 从上面可以看到…

    Java 2023年5月30日
    088
  • java.time.LocalDate格式化 及 LocalDate转Date

    undefined import java.time.LocalDate; import java.time.LocalDateTime; import java.time.Zon…

    Java 2023年5月29日
    079
  • [学习标准库]assert.h

    打算趁留在学校的最后一段时间好好补习一下一直以来都忽略掉的C/C++标准库,大概就是以头文件为单位了。以一个最简单的头文件入手,然后逐渐展开来……第一个头文…

    Java 2023年6月5日
    059
  • 打破双亲委派机制的自定义类加载器

    1.场景 mybatis的插件去做切换数据源 数据源存放在map中 mybatis的插件去map拿数据,发现数据丢失 2.springboot为了热加载自定义的 RestartCl…

    Java 2023年6月13日
    052
  • 背包

    1.最优子结构性质:最优解包含了其子问题的最优解,不是合并所有子问题的解,而是找最优的一条解线路,选择部分子最优解来达到最终的最优解。2.子问题重叠性质:先计算子问题的解,再由子问…

    Java 2023年6月5日
    067
  • 并发编程之:Lock

    大家好,我是小黑,一个在互联网苟且偷生的农民工。 在之前的文章中,为了保证在并发情况下多线程共享数据的线程安全,我们会使用synchronized关键字来修饰方法或者代码块,以及在…

    Java 2023年6月7日
    076
  • Java学习 (25) 对象篇(05)抽象类&接口

    抽象类 – 语法实例 注意点 具体讲解视频(狂神说Java) 接口 – 语法实例 具体讲解视频(狂神说Java) 抽象类 abstract修饰符可以用来修饰…

    Java 2023年6月8日
    066
  • JS跨域通信方法及SF相关问题

    iframe的跨域通信比较推荐的是采用信使的方式。基本地原理是在iframe的内部再创建一个iframe(称之为信使),父子页面轮询信使的window.name,父子页面各自使用变…

    Java 2023年6月7日
    053
  • 【进阶】Java8新特性的理解与应用

    【进阶】Java8新特性的理解与应用 前言 Java 8是Java的一个重大版本,是目前企业中使用最广泛的一个版本。 它支持函数式编程,新的Stream API 、新的日期 API…

    Java 2023年6月6日
    075
  • java.util.Properties

    java.util.Properties是对properties这类配置文件的映射。支持key-value类型和xml类型两种。、 key-value类型的配置文件大略长这样: 打…

    Java 2023年5月29日
    069
  • dubbo入门示例

    本文主要介绍阿里dubbo的基本使用,关于dubbo的相关基础概念请自行参考dubbo官网:http://www.dubbo.io dubbo是一个服务治理的框架,在如今大规模的分…

    Java 2023年6月16日
    056
  • 23.线程锁的使用

    不合理的设定临界区域,会让线程的调用失去意义。 1.不应该频繁的使用锁 2.减小锁使用的区域,线程公共资源之外 的资源 尽量不要放到临界区。 示例二:(不用线程) 示例三:(使用线…

    Java 2023年5月29日
    0116
  • linux指定端口失效原因分析

    在搭建centos7系统后开启防护墙,防火墙状态为active(开启), firewall-cmd –zone=public –add-port=8080/…

    Java 2023年6月14日
    087
  • Mybatis Generator配置详解

    参考:http://www.jianshu.com/p/e09d2370b796 http://mbg.cndocs.tk

    Java 2023年5月30日
    067
  • 共读《redis设计与实现》-单机(一)

    上一章我们讲了 redis 基本类型的 &#x6570;&#x636E;&#x7ED3;&#x6784; 和 &#x5BF9;&#x…

    Java 2023年6月7日
    083
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球