码迷,mamicode.com
首页 > 微信 > 详细

微信公众号创建菜单

时间:2016-11-14 12:08:09      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:app   log   读取   输出   puts   get   new   cgi   返回   

本方法目前用于测试号使用,需要提前知道Access_token与编辑好json字符串

    /**
     * 传入目录格式的json字符串跟安全密令,返回一个微信确认答复
     * @param params 编辑好的json的微信菜单字符串
     * @param accessToken 通过工具类返回的安全密令
     */
    public static void createMenu(String params,String accessToken) {
        StringBuffer bufferRes = new StringBuffer();
        try {
            //设置链接地址与参数
            URL realUrl = new URL("https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+ accessToken);
            //设置链接外网代理对象
             InetSocketAddress addr = new InetSocketAddress("192.168.99.100",80);            
             Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); 
             
             //创建连接
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection(proxy);
            // 连接超时
            conn.setConnectTimeout(25000);
            // 读取超时 --服务器响应比较慢,增大时间
            conn.setReadTimeout(25000);
            HttpURLConnection.setFollowRedirects(true);
            // 请求方式,允许输入输出流
            conn.setRequestMethod("GET");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            //链接
            conn.connect();
            // 获取URLConnection对象对应的输出流
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
            // 发送请求参数
            //out.write(URLEncoder.encode(params,"UTF-8"));
            //发送编辑好的目录格式的Json字符串
            out.write(params);
            out.flush();
            out.close();
            
            //取得微信端对于本次链接的返回值,确定是否链接成功
            InputStream in = conn.getInputStream();
            BufferedReader read = new BufferedReader(new InputStreamReader(in,"UTF-8"));
            String valueString = null;
            while ((valueString=read.readLine())!=null){
                bufferRes.append(valueString);
            }
            System.out.println(bufferRes.toString());
            in.close();
            if (conn != null) {
                // 关闭连接
                conn.disconnect();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

 

微信公众号创建菜单

标签:app   log   读取   输出   puts   get   new   cgi   返回   

原文地址:http://www.cnblogs.com/blackdeng/p/6060938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!