调用微信小程序接口获取不限制的小程序码,并将其保存到本地

调用微信小程序接口获取不限制的小程序码,并将其保存到本地


import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.nio.charset.StandardCharsets;

public class WxACodeUnlimitDownloader {
    // 微信小程序接口URL
    private static final String API_URL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit";

    public static void main(String[] args) {
        // 请替换为你的小程序access_token
        String accessToken = "";
        // 保存路径
        String savePath = "D:\\upload\\wxacode.png";
        // 自定义参数
        String scene = "winCode=w0001&dCode=d0003";
        // 页面路径
        String page = "pages/entryAndExit/entryAndExit";

        try {
            downloadWxACode(accessToken, scene, page, savePath);
            System.out.println("小程序码已成功保存到: " + savePath);
        } catch (Exception e) {
            System.err.println("获取小程序码失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 调用微信API获取小程序码并保存到本地
     * @param accessToken 接口调用凭证
     * @param scene 自定义参数
     * @param page 小程序页面路径
     * @param savePath 保存路径
     * @throws IOException 网络或文件操作异常
     */
    public static void downloadWxACode(String accessToken, String scene, String page, String savePath) throws IOException {
        // 构建请求URL
        URL url = new URL(API_URL + "?access_token=" + accessToken);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求头和请求方式
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");

        // 构建请求体JSON
        String requestBody = String.format("{\"scene\": \"%s\", \"page\": \"%s\"}", scene, page);

        // 发送请求
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);
            os.write(input, 0, input.length);
        }

        // 获取响应状态码
        int responseCode = connection.getResponseCode();

        // 根据状态码处理响应
        if (responseCode == HttpURLConnection.HTTP_OK) {
            // 成功响应(返回二进制图片)
            try (InputStream is = connection.getInputStream();
                 FileOutputStream fos = new FileOutputStream(savePath)) {
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, bytesRead);
                }
            }
        } else {
            // 错误响应(返回JSON错误信息)
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8))) {
                StringBuilder response = new StringBuilder();
                String responseLine;
                while ((responseLine = br.readLine()) != null) {
                    response.append(responseLine.trim());
                }
                throw new IOException("微信API返回错误: " + response);
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值