以下是一个使用Java编写的微信公众号主动推送消息给指定用户的示例代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class WeChatPushDemo {
    public static void main(String[] args) {
        // 替换为自己的公众号appID和appSecret
        String appID = "your_appID";
        String appSecret = "your_appSecret";
        
        // 替换为要发送消息的用户的openID
        String openID = "user_openID";
        
        // 替换为要发送的消息内容
        String message = "Hello, World!";
        
        try {
            // 获取access_token
            String accessToken = getAccessToken(appID, appSecret);
            
            // 发送消息
            sendTextMessage(accessToken, openID, message);
            
            System.out.println("消息发送成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    // 获取access_token
    public static String getAccessToken(String appID, String appSecret) throws Exception {
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
                + URLEncoder.encode(appID, "UTF-8") + "&secret=" + URLEncoder.encode(appSecret, "UTF-8");
        
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        
        // 发送GET请求
        con.setRequestMethod("GET");
        
        int responseCode = con.getResponseCode();
        
        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            
            // 解析返回的JSON数据
            String accessToken = response.toString().split("\"")[3];
            
            return accessToken;
        } else {
            throw new Exception("获取access_token失败,错误代码:" + responseCode);
        }
    }
    
    // 发送文本消息
    public static void sendTextMessage(String accessToken, String openID, String text) throws Exception {
        String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + URLEncoder.encode(accessToken, "UTF-8");
        
        String data = "{\"touser\":\"" + openID + "\",\"msgtype\":\"text\",\"text\":{\"content\":\"" + text + "\"}}";
        
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        
        // 发送POST请求
        con.setRequestMethod("POST");
        con.setDoOutput(true);
        con.getOutputStream().write(data.getBytes("UTF-8"));
        
        int responseCode = con.getResponseCode();
        
        if (responseCode != HttpURLConnection.HTTP_OK) {
            throw new Exception("发送消息失败,错误代码:" + responseCode);
        }
    }
}

请注意替换代码中的your_appIDyour_appSecretuser_openID为自己的实际值。此示例代码中使用了HTTP连接发送请求,建议在实际项目中使用更合适的HTTP客户端库,如Apache HttpClient或OkHttp。

用java写一个微信公众号主动推送消息给指定用户的demo

原文地址: http://www.cveoy.top/t/topic/i5v7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录