日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/13 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 微信高级群发_微信高级群发接口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();

}

}

總結

以上是生活随笔為你收集整理的java 微信高级群发_微信高级群发接口demo的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。