最近在开发activiti流程的时候有个需求:流程到达每个审批节点后,需要向该节点的审批人发送一个消息,提示有审批需要处理。

参考了一下微信的开发者文档和网络上的一些技术博客,现在记录一下。以便后续继续研究微信开发。【微信开发真的很好玩的】

首先,你需要注册一个企业微信账号:https://work.weixin.qq.com/wework_admin/register_wx?from=myhome

然后进入后台管理,创建一个应用

上面标红的信息,都是开发测试需要用到的,需要记录一下。

先贴代码:

WeChatData.java

package com.xfma.wx.test;/*** 微信消息发送实体类* @author PC-MXF**/
public class WeChatData {//发送微信消息的URLString sendMsgUrl="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";/*** 成员账号*/private String touser;/*** 消息类型*/private String msgtype;/*** 企业用用的agentid*/private String agentid;/*** 十几接收map类型数据*/private Object text;public String getTouser() {return touser;}public void setTouser(String touser) {this.touser = touser;}public String getMsgtype() {return msgtype;}public void setMsgtype(String msgtype) {this.msgtype = msgtype;}public String getAgentid() {return agentid;}public void setAgentid(String agentid) {this.agentid = agentid;}public Object getText() {return text;}public void setText(Object text) {this.text = text;}}

  WeChatUrlData.java

package com.xfma.wx.test;/*** 微信授权请求* @author PC-MXF**/
public class WeChatUrlData {/***  企业Id*/private String corpid;/*** secret管理组的凭证密钥*/private String corpsecret;/*** 获取ToKen的请求*/private String Get_Token_Url;/*** 发送消息的请求*/private String SendMessage_Url;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 String getGet_Token_Url() {return Get_Token_Url;}public void setGet_Token_Url(String corpid,String corpsecret) {Get_Token_Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+corpsecret;}public String getSendMessage_Url() {SendMessage_Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";return SendMessage_Url;}public void setSendMessage_Url(String sendMessage_Url) {SendMessage_Url = sendMessage_Url;}}

  WeChatMsgSend.java

package com.xfma.wx.test;import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.LoggerFactory;import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;/*** 微信发送消息* * @author PC-MXF**/
public class WeChatMsgSend {private CloseableHttpClient httpClient;/*** 用于提交登录数据*/private HttpPost httpPost;/*** 用于获得登陆后页面*/private HttpGet httpGet;public static final String CONTENT_TYPE = "Content-Type";SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");private static Gson gson = new Gson();/*** 微信授权请求,GET类型,获取授权响应,用于其他方法截取token* * @param Get_Token_Url* @return String 授权响应内容* @throws IOException*/protected String toAuth(String Get_Token_Url) throws IOException {httpClient = HttpClients.createDefault();httpGet = new HttpGet(Get_Token_Url);CloseableHttpResponse response = httpClient.execute(httpGet);String resp = "";try {HttpEntity entity = response.getEntity();resp = EntityUtils.toString(entity, "utf-8");EntityUtils.consume(entity);} catch (Exception e) {e.getStackTrace();} finally {response.close();}LoggerFactory.getLogger(getClass()).info(" resp:{}", resp);return resp;}/*** corpid应用组织编号 corpsecret应用秘钥 获取toAuth(String* Get_Token_Url)返回结果中键值对中access_token键的值* * @param*/public String getToken(String corpid, String corpsecret) throws IOException {WeChatMsgSend sw = new WeChatMsgSend();WeChatUrlData uData = new WeChatUrlData();uData.setGet_Token_Url(corpid, corpsecret);String resp = sw.toAuth(uData.getGet_Token_Url());System.out.println("resp=====:" + resp);try {Map<String, Object> map = gson.fromJson(resp, new TypeToken<Map<String, Object>>() {}.getType());return map.get("access_token").toString();} catch (Exception e) {e.getStackTrace();return resp;}}/*** 创建微信发送请求post数据 touser发送消息接收者 ,msgtype消息类型(文本/图片等), application_id应用编号。* 本方法适用于text型微信消息,contentKey和contentValue只能组一对* * @param touser* @param msgtype* @param application_id* @param contentKey* @param contentValue* @return*/public String createpostdata(String touser, String msgtype, int application_id, String contentKey,String contentValue) {WeChatData wcd = new WeChatData();wcd.setTouser(touser);wcd.setAgentid(application_id + "");wcd.setMsgtype(msgtype);Map<Object, Object> content = new HashMap<Object, Object>();content.put(contentKey, contentValue);wcd.setText(content);return gson.toJson(wcd);}/*** @Title  创建微信发送请求post实体,charset消息编码    ,contentType消息体内容类型,* url微信消息发送请求地址,data为post数据,token鉴权token* @param charset* @param contentType* @param url* @param data* @param token* @return* @throws IOException*/public 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();}LoggerFactory.getLogger(getClass()).info("call [{}], param:{}, resp:{}", url, data, resp);return resp;}
}

  Test.java

package com.xfma.wx.test;public class Test {public static void main(String[] args) {WeChatMsgSend swx = new WeChatMsgSend();try {String token = swx.getToken("ww44a4ede4d3626a","w6tRMGfN5WSFarMmqdpkwBztPRy-HVXRRs76QXvFU");String postdata = swx.createpostdata("jiangpin", "text", 1000009, "content","这是一条测试信息");String resp = swx.post("utf-8", WeChatMsgSend.CONTENT_TYPE,(new WeChatUrlData()).getSendMessage_Url(), postdata, token);System.out.println("获取到的token======>" + token);System.out.println("请求数据======>" + postdata);System.out.println("发送微信的响应数据======>" + resp);}catch (Exception e) {e.getStackTrace();}}
}

  

转载于:https://www.cnblogs.com/blog411032/p/9716813.html

java访问微信接口发送消息相关推荐

  1. java发微信_java访问微信接口发送消息

    最近在开发activiti流程的时候有个需求:流程到达每个审批节点后,需要向该节点的审批人发送一个消息,提示有审批需要处理. 参考了一下微信的开发者文档和网络上的一些技术博客,现在记录一下.以便后续继 ...

  2. C# 企业微信接口发送消息出现错误代码60020解决方案,希望能给大家带来帮助。

    这是企业微信接口发送消息调用的代码源地址. https://blog.csdn.net/wanglui1990/article/details/79744407 代码运行起来是没有问题的,但唯一出现的 ...

  3. java 调用微信api发送消息

    要在 Java 中调用微信 API 发送消息,你需要做的第一步是在微信公众平台中注册自己的公众号,然后获取到自己的 AppID 和 AppSecret. 然后你可以使用微信公众平台提供的开发文档,来了 ...

  4. python微信接口发送消息_Python 微信公众号发送消息

    #pip3 install requests importrequestsimportjsondefget_access_token():"""获取微信全局接口的凭证(默 ...

  5. python微信接口发送消息_调用微信API发送微信消息python脚本

    前阵子部署zabbix监控系统,做了个微信报警,下面分享下微信调API发消息的脚本.要用微信发消息,自己首先要有微信企业号,如果没有申请也容易 准备工作: 1.申请微信企业号 2.在企业号后台创建应用 ...

  6. java调用企业微信接口发送文件功能

    java调用企业微信接口发送文件功能 代码 结合几位大佬的代码后实现此功能:感谢大佬让我完成此功能的实现,如有侵权,立刻删除. 借鉴文章地址: java调用企业微信接口发送消息https://blog ...

  7. java 发送客服消息,Java调用微信客服消息实现发货通知的方法详解

    本文实例讲述了java调用微信客服消息实现发货通知的方法.分享给大家供大家参考,具体如下: 个人说明:这是一个样例,微信客户消息有很多种,我现在用的是公众号发送消息.样子如下图. 说明:下面开始代码部 ...

  8. java短信接口发送的这三种短信,你收到过几种?

    不同的行业应用java短信接口的用途不一样,但大多数都是用于传递消息.加强服务.提高安全性,因而一般情况下java短信接口会发送通知类短信.问候类短信.营销类短信及广告类短信,具体的让我们一起来了解下 ...

  9. WPF仿微信界面发送消息简易版

    WPF仿微信界面发送消息简易版 参考别的博主的例子用WPF MVVM框架来仿了一个微信聊天界面,做了个发送消息简易功能,下面一起来看看吧! 以下为View视图布局代码,消息对话框的样式直接在这里定义了 ...

最新文章

  1. Mybatis 获取当前序列和下一个序列值 以及在一个方法中写多条SQL 语句
  2. 零代码如何打造自己的实时监控预警系统
  3. chrome浏览器调试手机端h5页面
  4. SpringBoot(1.5.6.RELEASE)源码解析(一)
  5. 【算法学习笔记】哈夫曼树的构建和哈夫曼编码的实现代码
  6. 【LeetCode笔记】146. LRU缓存机制(Java、双向链表、哈希表)
  7. iOS之 开发常用到的宏定义
  8. mongodb自动备份脚本
  9. web前端(3)—— html标签及web页面结构
  10. 2018年1月19日 第七次小组会议
  11. python学习之路-书籍推荐
  12. php下的ssm模式,编码风格:Mvc模式下SSM环境,代码分层管理
  13. pytorch: Tensor的创建与调整
  14. IP defragment
  15. SVN—创建分支、合并分支到主干
  16. asterisk 服务器文档,用 Asterisk 搭建自己的免费 VoIP 服务器
  17. Python实现psf2otf
  18. Python利用Matplotlib绘图无法显示中文字体的解决方案
  19. 数据可视化:在 React 项目中使用 Vega 图表 (一)
  20. caffe 绘制acceracy曲线 IndexError: list index out of range的解决方案

热门文章

  1. 线性回归的从零开始实现-08-p3
  2. 三维点云学习(4)6-ransac 地面分割
  3. 100个最佳Linux站点
  4. docker兼容的linux内核,CentOS 6.5上安装Docker与Docker对中文字符集的支持
  5. spring aop如何在切面类中获取切入点相关方法的参数、方法名、返回值、异常等信息
  6. hdu 1686(标准的kmp,可当模板)
  7. 填表法解“银行家算法”问题
  8. 计算机专业课教学,计算机专业课教学的原则和方法
  9. Apache中配置ASP.NET环境
  10. [开源]quakeIII(雷神之锤3)源码