java实现自动发送微信QQ消息(可发图片)

                       自动发送微信/QQ/微博等消息

一:功能:实现发送QQ/微信/微博消息,程序控制。

效果演示(实现自动发生QQ微信消息给女友,发微博信息也可以就不演示了)

QQ发送消息

微信发送消息

二:原理讲解:将剪贴板的内容给发送到QQ微信中去,复制粘贴发送。

                      模拟真实操作进行发送信息。

 

三:前期准备,软件安装,及初步测试。(jdk/IDEA),

JDK请参考下面博客  https://blog.csdn.

实现微信自动回复需要使用微信公众平台接口进行开发,以下是实现的大致步骤: 1. 注册微信公众平台账号并获取开发者ID和开发者密码。 2. 在微信公众平台中创建公众号,并开启开发模式。 3. 编写Java程序,使用微信公众平台提供的接口实现自动回复功能。 4. 在程序中使用开发者ID和开发者密码进行认证并获取access_token。 5. 根据用户发送消息类型进行相应的处理并回复消息。 以下是一份Java微信自动回复的示例代码: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.Arrays; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import javax.net.ssl.HttpsURLConnection; import org.apache.commons.codec.binary.Base64; import org.json.JSONArray; import org.json.JSONObject; public class WeChatAutoReply { private static final String APPID = "your_appid"; private static final String APPSECRET = "your_appsecret"; private static final String TOKEN = "your_token"; public static void main(String[] args) { try { WeChatAutoReply autoReply = new WeChatAutoReply(); String message = autoReply.getMessage("hello"); System.out.println(message); } catch (Exception e) { e.printStackTrace(); } } public String getMessage(String content) throws Exception { String message = ""; String accessToken = getAccessToken(); String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken; JSONObject json = new JSONObject(); json.put("touser", "your_openid"); json.put("msgtype", "text"); JSONObject text = new JSONObject(); text.put("content", content); json.put("text", text); String data = json.toString(); String result = sendPost(url, data); JSONObject resultJson = new JSONObject(result); if (resultJson.getInt("errcode") == 0) { message = "发送成功"; } else { message = "发送失败"; } return message; } private String getAccessToken() throws Exception { String accessToken = ""; String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + APPID + "&secret=" + APPSECRET; String result = sendGet(url); JSONObject resultJson = new JSONObject(result); if (resultJson.has("access_token")) { accessToken = resultJson.getString("access_token"); } return accessToken; } private String sendGet(String url) throws Exception { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", "Mozilla/5.0"); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); } private String sendPost(String url, String data) throws Exception { URL obj = new URL(url); HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("Authorization", "Basic " + getAuthorization()); String urlParameters = data; con.setDoOutput(true); con.getOutputStream().write(urlParameters.getBytes("UTF-8")); InputStream inputStream = con.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); return response.toString(); } private String getAuthorization() throws Exception { String[] keys = new String[] { "grant_type", "appid", "secret" }; Arrays.sort(keys); StringBuilder sb = new StringBuilder(); for (String key : keys) { sb.append(key).append("=").append(URLEncoder.encode(getValue(key), "UTF-8")).append("&"); } sb.deleteCharAt(sb.length() - 1); String stringToSign = sb.toString(); String signature = hmacSha256(stringToSign, APPSECRET); return Base64.encodeBase64String((APPID + ":" + signature).getBytes("UTF-8")); } private String hmacSha256(String message, String secret) throws Exception { SecretKeySpec keySpec = new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(keySpec); byte[] rawHmac = mac.doFinal(message.getBytes("UTF-8")); return Base64.encodeBase64String(rawHmac); } private String getValue(String key) { if ("grant_type".equals(key)) { return "client_credential"; } else if ("appid".equals(key)) { return APPID; } else if ("secret".equals(key)) { return APPSECRET; } return null; } } ``` 在这段代码中,需要将`your_appid`、`your_appsecret`和`your_openid`替换为你的微信公众平台账号相关信息。同时,需要注意在使用微信公众平台接口时需要遵循微信公众平台开发规范,例如不要频繁调用API接口等。
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值