服务端生成小程序二维码、扫码可直接进小程序
分为两步、要生成小程序的二维码,都是调用小程序自己的api
1、先获取token
2、然后再生成二维码
HttpGet httpGet = new HttpGet( "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret ); HttpClient httpClient = HttpClients.createDefault(); HttpResponse res = httpClient.execute(httpGet); HttpEntity entity = res.getEntity(); String result = EntityUtils.toString(entity, "UTF-8"); JSONObject jsons = JSONObject.fromObject(result); String expires_in = jsons.getString("expires_in"); //缓存 if(Integer.parseInt(expires_in)==7200){ //ok String access_token = jsons.getString("access_token"); System.out.println("access_token:"+access_token);
}
URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("POST");// 提交模式 // 发送POST请求必须设置如下两行 httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); // 获取URLConnection对象对应的输出流 PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream()); // 发送请求参数 JSONObject paramJson = new JSONObject(); paramJson.put("scene", "a=1234567890"); paramJson.put("page", page); paramJson.put("width", 430); paramJson.put("auto_color", true); printWriter.write(paramJson.toString()); // flush输出流的缓冲 printWriter.flush(); //开始获取数据 bis = new BufferedInputStream(httpURLConnection.getInputStream()); bos = new ByteArrayOutputStream(); bos.write(bis); byte[] bs = bos.toByteArray(); printWriter.close();
bos.close();
bis.close();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?