zoukankan      html  css  js  c++  java
  • 获取小程序码

     public String yaoqing(){

        jsonresult = new JsonResult();

        String openid = request.getParameter("openid");

        Peizhi peizhi = DAO.findbyid(Peizhi.class, 1);

        String URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+peizhi.getAppid()+"&secret="+peizhi.getAppsecret()+"";

    //    String URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxe185e021a4367b1b&secret=067cc348c2c196fa245d350503f71d05";

        String text = CommonUtil.httpsRequest2(URL, "GET", null);

        Gson gson = new Gson();  

    Map<String, String> map = new HashMap<String, String>();  

    map = gson.fromJson(text, map.getClass());  

    String access_token = map.get("access_token");

    CommonUtil.xiaochengxuma(access_token,openid);

        return "jsonresult";

        }

    public static void xiaochengxuma(String access_token,String openid){
    try
    {
    URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token);
    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
    httpURLConnection.setRequestMethod("POST");// 提交模式
    // conn.setConnectTimeout(10000);//连接超时 单位毫秒
    // conn.setReadTimeout(2000);//读取超时 单位毫秒
    // 发送POST请求必须设置如下两行
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setDoInput(true);
    // 获取URLConnection对象对应的输出流
    PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
    // 发送请求参数
    JSONObject paramJson = new JSONObject();
    paramJson.put("scene", openid);
    paramJson.put("page", "pages/huoqu/huoqu");
    // paramJson.put("width", 430);
    // paramJson.put("auto_color", true);
    /**
    * line_color生效
    * paramJson.put("auto_color", false);
    * JSONObject lineColor = new JSONObject();
    * lineColor.put("r", 0);
    * lineColor.put("g", 0);
    * lineColor.put("b", 0);
    * paramJson.put("line_color", lineColor);
    * */

    printWriter.write(paramJson.toString());
    System.out.println(paramJson.toString());
    // flush输出流的缓冲
    printWriter.flush();
    //开始获取数据
    BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
    String root=ServletActionContext.getServletContext().getRealPath("upload");
    OutputStream os = new FileOutputStream(new File(root,openid.substring(2, 8)+".png"));
    int len;
    byte[] arr = new byte[1024];
    while ((len = bis.read(arr)) != -1)
    {
    os.write(arr, 0, len);
    os.flush();
    }
    os.close();
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }

  • 相关阅读:
    7. Reverse Integer
    4. Median of Two Sorted Arrays
    微服务实战系列(四)-注册中心springcloud alibaba nacos-copy
    微服务实战系列(五)-注册中心Eureka与nacos区别-copy
    Restful、SOAP、RPC、SOA、微服务之间的区别-copy
    从SOA到RPC、SOAP、REST-copy
    spring-session实现分布式集群session的共享-copy
    深入理解 RESTful Api 架构-copy
    RESTful 架构详解-copy
    SpringBoot使用MyBatis-Generator详解-copy
  • 原文地址:https://www.cnblogs.com/zcy1995/p/9361521.html
Copyright © 2011-2022 走看看