最近在开发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();
}
}
}

  

java访问微信接口发送消息的更多相关文章

  1. [实例]JAVA调用微信接口发送图文消息,不用跳到详情页

    package com.test; import java.io.IOException; import java.io.InputStream; import java.io.OutputStrea ...

  2. 全网最全的Windows下Python2 / Python3里正确下载安装用来向微信好友发送消息的itchat库(图文详解)

    不多说,直接上干货! 建议,你用Anaconda2或Anaconda3. 见 全网最全的Windows下Anaconda2 / Anaconda3里正确下载安装用来向微信好友发送消息的itchat库( ...

  3. java微信接口之五—消息分组群发

    一.微信消息分组群发接口简介 1.请求:该请求是使用post提交地址为: https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_t ...

  4. Java企业微信开发_05_消息推送之发送消息(主动)

    一.本节要点 1.发送消息与被动回复消息 (1)流程不同:发送消息是第三方服务器主动通知微信服务器向用户发消息.而被动回复消息是 用户发送消息之后,微信服务器将消息传递给 第三方服务器,第三方服务器接 ...

  5. Java企业微信开发_04_消息推送之发送消息(主动)

    源码请见: Java企业微信开发_00_源码及资源汇总贴 一.本节要点 1.发送消息与被动回复消息 (1)流程不同:发送消息是第三方服务器主动通知微信服务器向用户发消息.而被动回复消息是 用户发送消息 ...

  6. 通过企业微信API接口发送消息

    最近给公司测试组内部开发一个记账小工具,当账目出现问题的时候需要发送消息通知大家,前期主要采用的QQ发送通知消息,但是有一天突然无法连接到QQ服务器,运维的同学建议采用微信的方式对接然后进行告警,所以 ...

  7. Java企业微信开发_05_消息推送之被动回复消息

    一.本节要点 1.消息的加解密 微信加解密包 下载地址:http://qydev.weixin.qq.com/java.zip      ,此包中封装好了AES加解密方法,直接调用方法即可. 其中,解 ...

  8. Java实现 微信小程序 + 消息推送

    实现效果: 下面要显示五个字段 接下来,参照官方文档,一步步实现: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open- ...

  9. 使用WeCloud消息推送接口发送消息NodeJs版

    WeCloud是一家初创公司的产品,眼下主要在做Android和IOS消息推送这块.他们提供了用于向设备发送消息的协议,详细协议内容见消息推送协议. 这篇文章将使用NodeJs基于这个推送协议完毕向A ...

随机推荐

  1. 黑马程序猿——JAVA高新技术——反射

    ----------android培训.java培训.java学习型技术博客.期待与您交流!------------ 一.对于反射的概念 对于JAVA反射机制是在执行状态中,对于随意一个类.都可以知道 ...

  2. Python&lt;1&gt;List

    list里的元素以逗号隔开,以[]包围,当中元素的类型随意 官方一点的说:list列表是一个随意类型的对象的位置相关的有序集合. 它没有固定的大小(1).通过对偏移量 (2)进行赋值以及其它各种列表的 ...

  3. 网络编程readn、writen和readline函数的编写

    readn   在Linux中,read的声明为: ssize_t read(int fd, void *buf, size_t count); 它的返回值有以下情形: 1.大于0,代表成功读取的字节 ...

  4. C语言 | 计算器实现(中缀表示法/后缀表示法)

    ———————————————————————————————————————————— 实现原理: 每个操作数都被依次压入栈中,当一个运算符到达时,从栈中弹出相应数目的操作数(对于二元运算符来说是两 ...

  5. ClassLibary和WPF User Control LIbary和WPF Custom Control Libary的异同

    说来惭愧,接触WPF这么长时间了,今天在写自定义控件时遇到一个问题:运行界面中并没有显示自定义控件,经调试发现原来没有加载Themes中的Generic.xaml. 可是为什么在其他solution中 ...

  6. mysql 主从切换

    4)提升slave为master Stop slave: Reset master; Reset slave all; 在5.6.3版本之后 Reset slave; 在5.6.3版本之前 查看sla ...

  7. 基于多输出有序回归的年龄识别(CVPR_2016)

    作为学习记录,将所做PPT摘录如下: 网络结构: 网络结构描述: 网络工作流程: 损失函数计算: 亚洲人脸数据集: 参考代码:

  8. docker 查看容器挂载的目录

    $ docker inspect container_name | grep Mounts -A 20

  9. UVALive - 4255 - Guess (拓扑排序)

    Guess 题目传送:Guess 白书例题 注意拓扑排序时,,入度同一时候为0的前缀和须要赋值为同一个数(这个数能够随机取.由于前缀和是累加的,每个a的数值都仅仅和前缀和之差有关).,由于此时能够看成 ...

  10. Matlab中使用jython扩展功能

    Matlab中面向对象能力并不强,通过使用jython引擎能够对其功能扩展. 1 编辑classpath.txt增加jython.jar 在matlab中输入 which classpath.txt ...