1:获取公众号信息
2:设置ip白名单
3:java代码
package mon.test;/**@作者 yangshaoping.ex*@时间 /5/20*/import java.io.IOException;import java.io.InputStream;import java.io.OutputStreamWriter;import .HttpURLConnection;import .URL;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;import org.ponent;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;@Componentpublic class WechatMsg {public static void main(String[] args) {WechatMsg wm = new WechatMsg();wm.SendWeChatMsg(wm.getToken());}/*** 获取token** @return token*/public String getToken() {// 授予形式String grant_type = "client_credential";//应用IDString appid = "xxxx";//密钥String secret = "xxx";// 接口地址拼接参数String getTokenApi = "https://api./cgi-bin/token?grant_type=" + grant_type + "&appid=" + appid+ "&secret=" + secret;String tokenJsonStr = doGetPost(getTokenApi, "GET", null);System.out.println("获取到的tokenJsonStr : " + tokenJsonStr);JSONObject tokenJson = JSONObject.parseObject(tokenJsonStr);String token = tokenJson.get("access_token").toString();System.out.println("获取到的TOKEN : " + token);return token;}/**** 发送消息** @param token*/public void SendWeChatMsg(String token) {// 接口地址String sendMsgApi = "https://api./cgi-bin/message/template/send?access_token="+token;//openIdString toUser = "oNSNEw4zdWz_JJzQOkK_2KS2h7G4ysp";//""oNSNEw4zdWz_JJzQOkK_2KS2h764";//消息模板IDString template_id = "7kD4asxbYmGD0CpBsussa5O7q8WdhEXFlWfQ_g1xx6Oaw";//整体参数mapMap<String, Object> paramMap = new HashMap<String, Object>();//消息主题显示相关mapMap<String, Object> dataMap = new HashMap<String, Object>();//根据自己的模板定义内容和颜色dataMap.put("first",new DataEntity("杨少平测试","#173177"));dataMap.put("keyword1",new DataEntity("测试","#173177"));dataMap.put("keyword2",new DataEntity("-05-20" ,"#173177"));dataMap.put("keyword3",new DataEntity("-05-20" ,"#173177"));dataMap.put("remark",new DataEntity("申请成功","#173177"));paramMap.put("touser", toUser);paramMap.put("template_id", template_id);paramMap.put("data", dataMap);paramMap.put("url","/?tn=15007414_dg");//需要实现跳转网页的,可以添加下面一行代码实现跳转// paramMap.put("url","http://xxxxxx.html");System.out.println(doGetPost(sendMsgApi,"POST",paramMap));}/*** 调用接口 post* @param apiPath*/public String doGetPost(String apiPath,String type,Map<String,Object> paramMap){OutputStreamWriter out = null;InputStream is = null;String result = null;try{URL url = new URL(apiPath);// 创建连接HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setDoOutput(true);connection.setDoInput(true);connection.setUseCaches(false);connection.setInstanceFollowRedirects(true);connection.setRequestMethod(type) ; // 设置请求方式connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式connection.connect();if(type.equals("POST")){out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码out.append(JSON.toJSONString(paramMap));out.flush();out.close();}// 读取响应is = connection.getInputStream();int length = (int) connection.getContentLength();// 获取长度if (length != -1) {byte[] data = new byte[length];byte[] temp = new byte[512];int readLen = 0;int destPos = 0;while ((readLen = is.read(temp)) > 0) {System.arraycopy(temp, 0, data, destPos, readLen);destPos += readLen;}result = new String(data, "UTF-8"); // utf-8编码}} catch (IOException e) {e.printStackTrace();} finally {try {is.close();} catch (IOException e) {e.printStackTrace();}}return result;}}
4:效果