微信高级群发接口demo

本文提供了一段使用微信SDK实现文本群发的示例代码,包括获取access_token、获取关注者列表、创建群发XML并发送的过程。代码通过GET请求获取access_token,然后使用该token获取关注者的openid列表,最终构造并发送群发的XML数据。
微信群发接口的基本思路:

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();
    }
}


vb群发消息软件源码,本程序是用vb6在clswindow2.1基础上开发的,是对微信客户端进行自动化处理的,完全模拟手工操作,稳定且绝度安全,不会被封。懂代码的可以稍微修改变成定时群发,批量指定目标人群群发。绝对物超所值,代码浅显易懂。 这种非常实用的软件作者选择开源,不像一些同行,摸通了一点技术就藏着掖着,没有共享精神,作者再此完全开源,没有dll没有ocx,clswindow2.1库也是作者精心打造可以用于许多自动化操作方面,非常方便。 各位觉得好麻烦给5分好评。 下面附带一下clswindow2.1更新记录,算是2.1这版本的非正式发布吧。 '============================================================================================== '名 称:windows窗体控制类v2.1 '描 述:一个操作windows窗口的类,可对窗口进行很多常用的操作(类名为clsWindow) '使用范例:Dim window As New clsWindow ' window.GetWindowByTitle("计算器").closeWindow '编 程:sysdzw 原创开发,如果有需要对模块扩充或更新的话请邮箱发我一份,共同维护 '发布日期:2013/06/01 '博 客:http://blog.163.com/sysdzw ' http://blog.youkuaiyun.com/sysdzw 'Email :sysdzw@163.com 'QQ :171977759 '版 本:V1.0 初版 2012/12/03 ' V1.1 修正了几个正则相关的函数,调整了部分类结构 2013/05/28 ' V1.2 增加属性Caption,可以获取或设置当前标题栏 2013/05/29 ' V1.3 增加了方法Focus,可以激活当前窗口 2013/06/01 ' 增加了方法Left,Top,Width,Height,Move,处理窗口位置等 ' V1.4 增加了窗口位置调整的几个函数 2013/06/04 ' 增加了得到应用程序路径的函数AppName ' 增加了得到应用程序启动参数的函数AppCommandLine ' V1.5 增加了窗口最大最小化,隐藏显示正常的几个函数 2013/06/06 ' 增加了获取控件相关函数是否使用正则的参数UseRegExp默认F ' V1.6 将Left,Top函数改为属性,可获得可设置 2013/06/10 ' V1.7 增加函数:CloseApp 结束进程 2013/06/13 ' 修正了部分跟正则匹配相关的函数 ' 增加函数:GetElementTextByText ' 增加函数:GetElementHwndByText ' V1.8 增加函数:GetWindowByClassName 2013/06/26 ' 增加函数:GetWindowByClassNameEx ' 增加函数:GetWindowByAppName ' 增加私有变量hWnd_ ' 增加属性hWnd,可设置,单设置时候会检查,非法则设置为0 ' 更新GetWindowByTitleEx函数,使之可以选择性支持正则 ' 删除GetWindowByTitleRegExp函数,合并到上面函数 ' 增加SetFocus函数,调用Focus实现,为了是兼容VB习惯
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值