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/
转载文章受原作者版权保护。转载请注明原作者出处!