本方法目前用于测试号使用,需要提前知道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(); } }