微信群发接口的基本思路:

1.  获取关注者列表

2.  创建群发的XML

3.  发送群发的XML。

作者编写了一个文本群发的demo 供读者分享。

public class TestWeixinGroupSend {

public String getAccess_token(){
        String access_token=null;
        StringBuffer action =new StringBuffer();
        action.append("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential")
        .append("&appid=********")
        .append("&secret=****************");
        
        URL url;
        try {
            url = new URL(action.toString());
            HttpURLConnection http = (HttpURLConnection) url.openConnection();        
            http.setRequestMethod("GET");        
            http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");    
            http.setDoInput(true);
            InputStream is =http.getInputStream();
            int size =is.available();
            byte[] buf=new byte[size];
            is.read(buf);
            String resp =new String(buf,"UTF-8");
            JSONObject jsonObject =JSONObject.fromObject(resp);
            System.out.println("access_token:"+jsonObject.toString());
            Object object =jsonObject.get("access_token");
            if(object !=null){
                  access_token =String.valueOf(object);
            }
             return access_token;
        } catch (MalformedURLException e) {
            e.printStackTrace();
             return access_token;
            
        } catch (IOException e) {
            e.printStackTrace();
             return access_token;
        
        }
        
    }
    
    public  JSONArray  getOpenids(){
        JSONArray array =null;
        String urlstr ="https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID";
        urlstr =urlstr.replace("ACCESS_TOKEN", getAccess_token());
        urlstr =urlstr.replace("NEXT_OPENID", "");
        URL url;
        try {
            url = new URL(urlstr);
            HttpURLConnection http = (HttpURLConnection) url.openConnection();        
            http.setRequestMethod("GET");        
            http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");    
            http.setDoInput(true);
            InputStream is =http.getInputStream();
            int size =is.available();
            byte[] buf=new byte[size];
            is.read(buf);
            String resp =new String(buf,"UTF-8");
            JSONObject jsonObject =JSONObject.fromObject(resp);
            System.out.println("resp:"+jsonObject.toString());
            array =jsonObject.getJSONObject("data").getJSONArray("openid");
            return array;
        } catch (MalformedURLException e) {
            e.printStackTrace();
             return array;
            
        } catch (IOException e) {
            e.printStackTrace();
             return array;
        
        }
    }
    @Test
    public void testsendTextByOpenids(){
        String urlstr ="https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN";
        String reqjson =createGroupText(getOpenids());
        try {
            
            URL httpclient =new URL(urlstr);
            HttpURLConnection conn =(HttpURLConnection) httpclient.openConnection();
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(2000);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");    
            conn.setDoOutput(true);        
            conn.setDoInput(true);
            conn.connect();
            OutputStream os= conn.getOutputStream();    
            System.out.println("req:"+reqjson);
            os.write(reqjson.getBytes("UTF-8"));//传入参数    
            os.flush();
            os.close();
            
            InputStream is =conn.getInputStream();
            int size =is.available();
            byte[] jsonBytes =new byte[size];
            is.read(jsonBytes);
            String message=new String(jsonBytes,"UTF-8");
            System.out.println("resp:"+message);
        
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    private String createGroupText(JSONArray array){
         JSONObject gjson =new JSONObject();
         gjson.put("touser", array);
         gjson.put("msgtype", "text");
         JSONObject text =new JSONObject();
         text.put("content", "hello from boxer.");
         gjson.put("text", text);
        return gjson.toString();
    }
}

够买教材与源码工程,2包烟的价格:

点击购买

咨询QQ:(928271079,提供优质的服务)

微信高级群发接口demo相关推荐

  1. java 微信高级群发_微信高级群发接口demo

    微信群发接口的基本思路: 1.  获取关注者列表 2.  创建群发的XML 3.  发送群发的XML. 作者编写了一个文本群发的demo 供读者分享. public class TestWeixinG ...

  2. 调用微信高级群发接口--视频群发接口出问题(微信官方文档错误纠正)

    这几天在弄项目与微信对接,我主要负责将素材(视频,图片.缩略图,音频)材料上传到微信server上.并推送到所关注本平台的用户中.从获取accessToken到素材上传.下载.或者关注者的openId ...

  3. java 微信高级群发_Java微信高级群发接口demo--Java学习网

    微信群发接口的基本思路:1.  获取关注者列表2.  创建群发的XML3.  发送群发的XML.作者编写了一个文本群发的demo 供读者分享.public class TestWeixinGroupS ...

  4. php 公众号 群发,微信公众号【服务号】群发策略调整,并开通高级群发接口

    刚刚看到这条消息,现在发可能有点晚,大家可能都知道了. 2014年4月15号,微信公众平台发布了<服务号群发策略调整>,对 服务号 的用户来说应该是一个好消息. 调整内容如下: 为了增强公 ...

  5. 微信公众平台高级群发接口(转载)

    出自微信公众平台开发者文档 在公众平台网站上,为订阅号提供了每天一条的群发权限,为服务号提供每月(自然月)4条的群发权限.而对于某些具备开发能力的公众号运营者,可以通过高级群发接口,实现更灵活的群发能 ...

  6. 微信开发教程(4)——高级群发接口

    在这篇微信公众平台高级接口开发教程中我们将介绍如何使用接口实现微信公众平台群发功能. 本文分为以下四个部分 准备群发内容 选择群发对象 执行群发 接收群发结果 一.准备群发内容 群发内容可以是文本.图 ...

  7. C#微信公众平台开发—高级群发接口

    涉及access_token的获取请参考<C#微信公众平台开发-access_token的获取存储与更新> 一.为了实现高级群发功能,需要解决的问题 1.通过微信接口上传图文消息素材时,J ...

  8. 微信公众平台开发 高级群发接口

    在这篇微信公众平台高级接口开发教程中,我们将介绍如何使用接口实现微信公众平台群发功能.本文分为以下四个部分:准备群发内容选择群发对象执行群发接收群发结果一.准备群发内容群发内容可以是文本.图片.语音. ...

  9. 微信开发----群发接口

    实现群发的接口很简单: 1.首先获取到access_token 2.获取到关注该公众号的所有的openid 3.调用群发的接口实现 <?php /*     Author:yf     使用说明 ...

最新文章

  1. VMware虚拟机提示在该系统上全局禁用了虚拟机打印功能
  2. 网规:第1章计算机网络原理-1.6广域网与接入
  3. LruCache缓存bitmap(一)
  4. APP:分享六款非常实用的冷门APP软件,值得一试!
  5. 【columnstore】mariadb columnstore 数据迁移
  6. MSP430F5529 DriverLib 库函数学习笔记(二)GPIO
  7. jQuery如何在线导入js包
  8. docker 删除所有镜像_Docker常用命令
  9. 【转】使用EBNF相对于BNF表示的优越性
  10. 中国水密门市场趋势报告、技术动态创新及市场预测
  11. 8.configurable product
  12. CS231n李飞飞计算机视觉 神经网络训练细节part2上
  13. _stdcall与_cdecl区别
  14. Protel99se中文版PCB负片输出
  15. G-sensor 介绍
  16. win10系统盘清理彻底的方法
  17. 鸡啄米C++和MFC学习网址链接
  18. 期货基本面分析:,马来西亚10月1-10日棕榈油出口量较上月同期下降17.3%,但对中国出口创一年新高
  19. 浏览器主页被搜狗劫持如何处理
  20. 怎么把图片中的文字转换成word

热门文章

  1. 如何升级手机android系统,安卓手机系统怎么升级,安卓手机系统升级教程
  2. 20大数据可视化工具测评
  3. unity3d布娃娃2
  4. case when 和 coalesce
  5. 新一代智能门禁解决方案
  6. 2044:【例5.12】回文字串
  7. Mysql 根据身份证号码操作
  8. 最佳引脚使用 – ESP8266 ESP12S
  9. wp网站,wp网站快速搭建,wordpress网站
  10. AD20元器件库及加载(二)