微信公众号客服消息提醒

2018-07-19 05:37:56来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

微信客服消息提醒:
    public static void Test(String openid) throws Exception{   
        String token = getWeiXinAccessToken.getToken();
        String strJson = "{\"touser\" :\""+openid+"\",";
        strJson += "\"msgtype\":\"text\",";
        strJson += "\"text\":{";
        strJson += "\"content\":\"这里写你要提示的信息!\"";
        strJson += "}}";
        String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?&body=0&access_token=" + token;
        post(url, strJson);
    }

    public static void post(String url, String json){
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        try{
            StringEntity s = new StringEntity(json.toString(),"UTF-8");
            //s.setContentEncoding("UTF-8");
            s.setContentType("application/json");
            post.setEntity(s);
            HttpResponse res = client.execute(post);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                HttpEntity entity = res.getEntity();
                System.out.println(EntityUtils.toString(entity, "utf-8"));
            }
        }catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

 public class getWeiXinAccessToken {
      public static  String getToken() {  
             String getResponse="";
             String appid= "公众号AppID";
             String secret= "公众号secret";
             try{  
                    String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret;  
                       HttpClient httpClient = new HttpClient();  
                    GetMethod getMethod = new GetMethod(url);  
                  int execute = httpClient.executeMethod(getMethod);  
                  System.out.println("execute:"+execute);  
                    getResponse = getMethod.getResponseBodyAsString(); 
            } catch (IOException e){  
                e.printStackTrace();  
            }  
            System.out.println(getResponse);  
            JSONObject json=JSONObject.fromObject(getResponse);      
            return json.getString("access_token");  
       }  
    
    } 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:springmvc的拦截器

下一篇:巧用ELK快速实现网站流量监控可视化