请求微信接口获取小程序码

public Map<String,Object> getSharePhoto(Map<String, Object> conditions){
        Map<String, Object> results = new HashMap<String, Object>();
        results.put("success",false);
        String userId = "123456464";
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpClient client = new DefaultHttpClient();
        try {
            //获取小程序token
            Map params = new HashMap();
            params.put("appid","appid");//小程序appid
            params.put("secret","secret");//小程序secret
            params.put("grant_type","client_credential");
            String returnMsg = null;
            String token = null;
            returnMsg = WhatyHttpClient.doGet("https://api.weixin.qq.com/cgi-bin/token", params);
            net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(returnMsg);
            if (jsonObject.get("access_token") != null){
                token = jsonObject.get("access_token").toString();
            }else {
                results.put("success",false);
                results.put("info","获取小程序access_token失败");
            }
            String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+token;
            HttpPost post = new HttpPost(url);
            JSONObject json = new JSONObject();
            json.put("scene",userId);
            json.put("is_hyaline",true); //透明小程序码
            //json.put("page","pages/test/test");
            post.setHeader("Content-Type", "application/json");
            post.addHeader("Authorization", "Basic YWRtaW46");
            StringEntity s = new StringEntity(json.toString(), "utf-8");
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            post.setEntity(s);
            // 发送请求
            HttpResponse httpResponse = client.execute(post);
            // 获取响应输入流
            InputStream inStream = httpResponse.getEntity().getContent();
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                String photoLink = null; // 文件上传目录
                photoLink = Const.FILE_PATH + userSession.getId() + ".png"; //整个路径加文件名
                File savedFile = new File(ServletActionContext.getServletContext().getRealPath(Const.FILE_PATH) + "/" + userSession.getId()
                        + ".png");
                FileOutputStream fileOut = new FileOutputStream(savedFile);
                BufferedOutputStream dataOut=new BufferedOutputStream (fileOut);
                ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
                byte[] buff = new byte[100];
                int rc = 0;
                while ((rc = inStream.read(buff, 0, 100)) > 0) {
                    swapStream.write(buff, 0, rc);
                }
                byte[] in2b = swapStream.toByteArray();
                dataOut.write(in2b);
                dataOut.flush();
                inStream.close();
                dataOut.close();
                results.put("success",true);
                results.put("photoLink",photoLink);
            } else {
                results.put("info","请求服务器失败");
            }
        }catch (Exception e){
            results.put("success",false);
            results.put("info","获取小程序码失败");
            e.printStackTrace();
            return results;
        }
        return results;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值