package com.bj58.ecat.emc.commutils; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.lang3.StringUtils; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.esotericsoftware.minlog.Log; /** * * @ClassName: TestWeixinGroupSend * @Description: 微信分批推送 * @author 58 * @date 2015-12-9 * */ public class TestWeixinGroupSend { private static int id = 0; /** * 读取微信 Token的url */ private final String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"; /** * 识别公众号 身份的appid,与 secret 一起作为用户身份标识 */ private final String APPID="xxxxxx"; /** * 识别公众号 身份的secret,与appid一起作为用户身份标识 */ private final String SECRET="xxxxxxxx"; Logger logger = LoggerFactory.getLogger(this.getClass()); public static void main(String[] args) { String t = new TestWeixinGroupSend().getAccess_token(); System.out.println(t); } /** * 获取指定的 Token * @Title: getAccess_token * @Description: TODO(获取指定的 Token) * @param @return 参数 * @return String 返回类型 * @throws */ public String getAccess_token(){ String access_token=null; StringBuffer action =new StringBuffer(); //构造读取Token的url action.append(GET_TOKEN_URL) .append("&appid=") .append(APPID) .append("&secret=") .append(SECRET); Log.info(this.getClass() + " get Token Url " + action.toString()); 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); 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; } } /** * 获取 关注者列表 * @Title: getOpenids * @Description: TODO(获取 关注者列表) * @param @return 参数 * @return JSONArray 返回类型 * @throws */ public JSONArray getOpenids(String next_openid){ 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()); /* * next_openid 上一次拉取列表的最后一个用户的OPENID,同时会作为下一次拉取的起始地址。 */ if(StringUtils.isBlank(next_openid)){ urlstr =urlstr.replace("NEXT_OPENID", ""); } else { urlstr =urlstr.replace("NEXT_OPENID",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); 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="; String reqjson =createGroupText(getOpenids("")); try { urlstr = urlstr + getAccess_token(); System.out.println(" urlstr ====>>>>> "+urlstr); 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(); } }
时间: 2024-10-07 17:00:51