微信公众号发送链接

   private void sendCustomWelcomeMessage(String openId) {
        // 构建图文消息的数据结构
        JSONObject article = new JSONObject();
        article.put("title", "首次微信登录,请先验证");
        article.put("description", "您好,首次微信登录,请先验证是否已完成注册。请点击这里进行验证:");
        article.put("url", "换成自己的链接?openId=" + openId);
        article.put("picurl", "照片url阿里云就行"); // 设置图片URL

        JSONObject news = new JSONObject();
        news.put("articles", new JSONObject[]{article});

        // 构建整个消息体
        JSONObject message = new JSONObject();
        message.put("touser", openId);
        message.put("msgtype", "news");
        message.put("news", news);

        // 发送消息的逻辑(通过微信客服消息接口发送)
        sendJsonMessageToUsers(message);

        System.out.println("欢迎消息已发送给用户,OpenID: " + openId);
    }

   private void sendJsonMessageToUsers(JSONObject message) {
        try {
            // 获取AccessToken
            String accessToken = getAccessTokenA();
            // 拼接URL
            String url = String.format(
                    "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s",
                    accessToken
            );
            // 构建JSON消息体
            String jsonPayload = message.toString();

            // 打印调试日志
            System.out.println("发送消息的URL:" + url);
            System.out.println("发送消息的内容:" + jsonPayload);

            // HTTP请求
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new StringEntity(jsonPayload, StandardCharsets.UTF_8));

            // 使用 Try-With-Resources 自动管理资源关闭
            try (CloseableHttpClient httpClient = HttpClients.createDefault();
                 CloseableHttpResponse response = httpClient.execute(httpPost)) {

                int statusCode = response.getStatusLine().getStatusCode();

                // 检查状态码
                if (statusCode == 200) {
                    System.out.println("消息发送成功!");
                } else {
                    System.err.println("消息发送失败,状态码:" + statusCode);
                    String responseContent = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                    System.err.println("响应内容:" + responseContent);
                }
            }
        } catch (CustomAesException e) {
            // 捕获自定义异常
            System.err.println("获取 AccessToken 时出错:" + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            // 捕获 HTTP 请求相关异常
            System.err.println("发送 HTTP 请求时出错:" + e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            // 捕获所有其他异常
            System.err.println("发送消息时发生未知错误:" + e.getMessage());
            e.printStackTrace();
        }
    }

在这里插入图片描述

    public static String getAccessTokenA() throws CustomAesException {
        // 获取完整的 access_token JSON 响应
        String jsonResponse = HttpUtils.sendSSLGet(
                accessTokenUrl,
                "grant_type=" + grant_type + "&appid=" + appId + "&secret=" + appSecre,
                Constants.UTF8
        );

        try {
            // 解析 JSON 响应
            JSONObject jsonObject = JSONObject.parseObject(jsonResponse);
            String accessToken = jsonObject.getString("access_token");

            if (accessToken == null || accessToken.isEmpty()) {
                throw new RuntimeException("未能成功提取 access_token: " + jsonResponse);
            }

            // 打印日志以调试
            System.out.println("提取的 access_token: " + accessToken);

            return accessToken; // 返回纯 access_token 字符串
        } catch (Exception e) {

        }
        return jsonResponse;
    }
   private static final String appId = "";
   private static final String appSecre = "";
   private static final String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token";

   private static final String ticketUrl = "https://api.weixin.qq.com/cgi-bin/qrcode/create";

   private static final String qrCodeUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode";
   private static final String grant_type = "client_credential";
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值