Loading

微信 创建自定义菜单 向微信发起的post请求

微信 创建自定义菜单 向微信发起的post请求

        Map<String, Object> res = new HashMap<String, Object>();
        try {
            String accessToken = accessTokenService.getAccessToken();
            if ("".equals(accessToken)) {
                res.put("res", "获取微信access_token失败,请与管理员联系");
                return res;
            }
            String action = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken;
            URL url = new URL(action);
            HttpURLConnection http = (HttpURLConnection) url.openConnection();
            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
            http.connect();
            OutputStream os = http.getOutputStream();
            os.write(data.getBytes("UTF-8"));// 传入参数
            os.flush();
            os.close();
            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] jsonBytes = new byte[size];
            is.read(jsonBytes);
            String message = new String(jsonBytes, "UTF-8");
            System.out.println(message);
            res.put("res", "成功创建自定义菜单。请取消关注然后再次关注查看自定义菜单结果。");
        } catch ( Exception e) {
            e.printStackTrace();
            res.put("res", "创建自定义菜单失败,错误信息"+e.getMessage()+",请与管理员联系。");
        } 
        

学习:

http://www.cnblogs.com/Leo_wl/p/3248862.html

 

应该可以改进吧;

 

posted @ 2017-03-24 07:17  stono  阅读(1004)  评论(0编辑  收藏  举报