1. 程式人生 > >Java生成無限制帶參小程式碼

Java生成無限制帶參小程式碼

官方文件:獲取二維碼

這裡使用其中的介面B:
這裡寫圖片描述

欄位含義寫的很清楚,需要注意的是page是小程式中已釋出頁面,且不能攜帶引數,引數放在scene中。請求成功的話微信伺服器返回的是輸入流,需要自行儲存,以下看程式碼:

public class Test
{
    public static void main(String[] args)
    {
        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", "a=1234567890"
); paramJson.put("page", "pages/index/index"); 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()); // flush輸出流的緩衝 printWriter.flush(); //開始獲取資料 BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream()); OutputStream os = new FileOutputStream(new File("/Users/Xxxx/Music/abc.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(); } } }