在JAVA中先編寫url請求的工具類:UrlUtil,代碼如下:
package com.sinotrans.agent.basic.service.impl; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.Iterator; import java.util.Map; public class UrlUtil { /** * 向指定 URL 發送POST方法的請求 * * @param url * 發送請求的 URL * @param param * 請求參數 * @return 所代表遠程資源的響應結果 */ public static String sendPost(String url, Map<String, ?> paramMap) { PrintWriter out = null; BufferedReader in = null; String result = ""; String param = ""; Iterator<String> it = paramMap.keySet().iterator(); while (it.hasNext()) { String key = it.next(); param += key + "=" + paramMap.get(key) + "&"; } try { URL realUrl = new URL(url); // 打開和URL之間的連接 URLConnection conn = realUrl.openConnection(); // 設置通用的請求屬性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("Accept-Charset", "utf-8"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; windows NT 5.1;SV1)"); // 發送POST請求必須設置如下兩行 conn.setDoOutput(true); conn.setDoInput(true); // 獲取URLConnection對象對應的輸出流 out = new PrintWriter(conn.getOutputStream()); // 發送請求參數 out.print(param); // flush輸出流的緩沖 out.flush(); // 定義BufferedReader輸入流來讀取URL的響應 in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println(e); } // 使用finally塊來關閉輸出流、輸入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; } }
然后編寫獲取token的方法,
public static String getToken() { try { Map<String, String> map = new LinkedHashMap<String, String>(); map.put("grant_type", "client_credential"); map.put("Appid", "要鏈接到小程序的id");// 改成自己的appid map.put("secret", "要鏈接小程序的secret"); //改成自己的secret String rt = UrlUtil.sendPost("https://api.weixin.qq.com/cgi-bin/token", map); JSONObject json = JSONObject.parseobject(rt); if (json.getString("access_token") != null || json.getString("access_token") != "") { System.out.println("token:" + json.getString("access_token")); return json.getString("access_token"); } else { return null; } } catch (Exception e) { e.printStackTrace(); return null; } }
接下來獲取二維碼的方法,為了測試,我將生成二維碼流打印到了自己本地的D盤。看看能不能掃描成功,答案是可以:
//sceneStr:鏈接到改小程序界面所要的參數 //accessToken:上一個方法中所生產的token public static Map getminiqrQr(String sceneStr, String accessToken) { RestTemplate rest = new RestTemplate(); InputStream inputStream = null; OutputStream outputStream = null; try { String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken; Map<String, Object> param = new HashMap<>(); param.put("scene", sceneStr); param.put("page", "pages/index/index"); param.put("width", 430); param.put("auto_color", false); MultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); HttpEntity requestEntity = new HttpEntity(param, headers); ResponseEntity<byte[]> entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]); System.out.println("調用小程序生成微信永久小程序碼URL接口返回結果:" + entity.getBody()); byte[] result = entity.getBody(); inputStream = new ByteArrayInputStream(result); File file = new File("D:/Desktop/3.png"); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } outputStream = new FileOutputStream(file); int len = 0; byte[] in_b = null; byte[] buf = new byte[1024]; while ((len = inputStream.read(buf, 0, 1024)) != -1) { outputStream.write(buf, 0, len); } outputStream.flush(); } catch (Exception e) { System.out.println("調用異常"); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }
添加main方法測試:
public static void main(String[] args) { String token = getToken(); getminiqrQr("1023", token); }
獲取token,二維碼的方法,以及測試的main方法,放到一個類中即可,復制可運行,但得導入相應的jar包。
這就生成二維碼了,具體獲取token.二維碼的參數,可以到小程序官網進行查看,此測試只填寫了必須的參數。
(小程序獲取二維碼官網:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
獲取token:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html)
作者:JO安
原文:https://blog.csdn.net/qq_40065816/article/details/91411228