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

微信群发接口的基本思路:1.  获取关注者列表2.  创建群发的XML3.  发送群发的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;}}@Testpublic 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();}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值