栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

微信公众号-推发消息

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微信公众号-推发消息

 

1:获取公众号信息

2:设置ip白名单

3:java代码

package com.eleadmin.common.test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

@Component
public class WechatMsg {

    public static void main(String[] args) {
        WechatMsg wm = new WechatMsg();
        wm.SendWeChatMsg(wm.getToken());
    }
    
    public String getToken() {
        // 授予形式
        String grant_type = "client_credential";
        //应用ID
        String appid = "xxxx";
        //密钥
        String secret = "xxx";
        // 接口地址拼接参数
        String getTokenApi = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" + appid
                + "&secret=" + secret;
        String tokenJsonStr = doGetPost(getTokenApi, "GET", null);
        System.out.println("获取到的tokenJsonStr : " + tokenJsonStr);
        JSONObject tokenJson = JSONObject.parseObject(tokenJsonStr);
        String token = tokenJson.get("access_token").toString();
        System.out.println("获取到的TOKEN : " + token);
        return token;
    }
    
    public void SendWeChatMsg(String token) {
        // 接口地址
        String sendMsgApi = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token;
        //openId
        String toUser = "oNSNEw4zdWz_JJzQOkK_2KS2h7G4ysp";//""oNSNEw4zdWz_JJzQOkK_2KS2h764";
        //消息模板ID
        String template_id = "7kD4asxbYmGD0CpBsussa5O7q8WdhEXFlWfQ_g1xx6Oaw";
        //整体参数map
        Map paramMap = new HashMap();
        //消息主题显示相关map
        Map dataMap = new HashMap();
        //根据自己的模板定义内容和颜色
        dataMap.put("first",new DataEntity("杨少平测试","#173177"));
        dataMap.put("keyword1",new DataEntity("测试","#173177"));
        dataMap.put("keyword2",new DataEntity("2022-05-20" ,"#173177"));
        dataMap.put("keyword3",new DataEntity("2022-05-20" ,"#173177"));
        dataMap.put("remark",new DataEntity("申请成功","#173177"));
        paramMap.put("touser", toUser);
        paramMap.put("template_id", template_id);
        paramMap.put("data", dataMap);
        paramMap.put("url","https://www.baidu.com/?tn=15007414_dg");
        //需要实现跳转网页的,可以添加下面一行代码实现跳转
        // paramMap.put("url","http://xxxxxx.html");
        System.out.println(doGetPost(sendMsgApi,"POST",paramMap));
    }
    
    public String doGetPost(String apiPath,String type,Map paramMap){
        OutputStreamWriter out = null;
        InputStream is = null;
        String result = null;
        try{
            URL url = new URL(apiPath);// 创建连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestMethod(type) ; // 设置请求方式
            connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
            connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
            connection.connect();
            if(type.equals("POST")){
                out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
                out.append(JSON.toJSONString(paramMap));
                out.flush();
                out.close();
            }
            // 读取响应
            is = connection.getInputStream();
            int length = (int) connection.getContentLength();// 获取长度
            if (length != -1) {
                byte[] data = new byte[length];
                byte[] temp = new byte[512];
                int readLen = 0;
                int destPos = 0;
                while ((readLen = is.read(temp)) > 0) {
                    System.arraycopy(temp, 0, data, destPos, readLen);
                    destPos += readLen;
                }
                result = new String(data, "UTF-8"); // utf-8编码
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return  result;
    }
}

4:效果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/921750.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号