微信高级群发接口demo
微信群发接口的基本思路:
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相关推荐
- java 微信高级群发_微信高级群发接口demo
微信群发接口的基本思路: 1. 获取关注者列表 2. 创建群发的XML 3. 发送群发的XML. 作者编写了一个文本群发的demo 供读者分享. public class TestWeixinG ...
- 调用微信高级群发接口--视频群发接口出问题(微信官方文档错误纠正)
这几天在弄项目与微信对接,我主要负责将素材(视频,图片.缩略图,音频)材料上传到微信server上.并推送到所关注本平台的用户中.从获取accessToken到素材上传.下载.或者关注者的openId ...
- java 微信高级群发_Java微信高级群发接口demo--Java学习网
微信群发接口的基本思路:1. 获取关注者列表2. 创建群发的XML3. 发送群发的XML.作者编写了一个文本群发的demo 供读者分享.public class TestWeixinGroupS ...
- php 公众号 群发,微信公众号【服务号】群发策略调整,并开通高级群发接口
刚刚看到这条消息,现在发可能有点晚,大家可能都知道了. 2014年4月15号,微信公众平台发布了<服务号群发策略调整>,对 服务号 的用户来说应该是一个好消息. 调整内容如下: 为了增强公 ...
- 微信公众平台高级群发接口(转载)
出自微信公众平台开发者文档 在公众平台网站上,为订阅号提供了每天一条的群发权限,为服务号提供每月(自然月)4条的群发权限.而对于某些具备开发能力的公众号运营者,可以通过高级群发接口,实现更灵活的群发能 ...
- 微信开发教程(4)——高级群发接口
在这篇微信公众平台高级接口开发教程中我们将介绍如何使用接口实现微信公众平台群发功能. 本文分为以下四个部分 准备群发内容 选择群发对象 执行群发 接收群发结果 一.准备群发内容 群发内容可以是文本.图 ...
- C#微信公众平台开发—高级群发接口
涉及access_token的获取请参考<C#微信公众平台开发-access_token的获取存储与更新> 一.为了实现高级群发功能,需要解决的问题 1.通过微信接口上传图文消息素材时,J ...
- 微信公众平台开发 高级群发接口
在这篇微信公众平台高级接口开发教程中,我们将介绍如何使用接口实现微信公众平台群发功能.本文分为以下四个部分:准备群发内容选择群发对象执行群发接收群发结果一.准备群发内容群发内容可以是文本.图片.语音. ...
- 微信开发----群发接口
实现群发的接口很简单: 1.首先获取到access_token 2.获取到关注该公众号的所有的openid 3.调用群发的接口实现 <?php /* Author:yf 使用说明 ...
最新文章
- VMware虚拟机提示在该系统上全局禁用了虚拟机打印功能
- 网规:第1章计算机网络原理-1.6广域网与接入
- LruCache缓存bitmap(一)
- APP:分享六款非常实用的冷门APP软件,值得一试!
- 【columnstore】mariadb columnstore 数据迁移
- MSP430F5529 DriverLib 库函数学习笔记(二)GPIO
- jQuery如何在线导入js包
- docker 删除所有镜像_Docker常用命令
- 【转】使用EBNF相对于BNF表示的优越性
- 中国水密门市场趋势报告、技术动态创新及市场预测
- 8.configurable product
- CS231n李飞飞计算机视觉 神经网络训练细节part2上
- _stdcall与_cdecl区别
- Protel99se中文版PCB负片输出
- G-sensor 介绍
- win10系统盘清理彻底的方法
- 鸡啄米C++和MFC学习网址链接
- 期货基本面分析:,马来西亚10月1-10日棕榈油出口量较上月同期下降17.3%,但对中国出口创一年新高
- 浏览器主页被搜狗劫持如何处理
- 怎么把图片中的文字转换成word