微信公发送推送消息java
时间: 2023-08-23 15:06:32 浏览: 87
java消息推送
要通过 Java 发送微信公众号推送消息,可以使用官方提供的接口和第三方库实现。以下是一个使用官方接口实现的示例代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
public class WechatPush {
public static void main(String[] args) {
// 填写公众号的 appid 和 appsecret
String appid = "your_appid";
String appsecret = "your_appsecret";
// 获取 access_token
String accessToken = getAccessToken(appid, appsecret);
// 发送推送消息
String url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken;
String postData = "{\"touser\":\"openid\",\"msgtype\":\"text\",\"text\":{\"content\":\"Hello World\"}}";
String result = sendPost(url, postData);
System.out.println(result);
}
/**
* 获取 access_token
*/
public static String getAccessToken(String appid, String appsecret) {
String accessToken = null;
try {
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret;
String result = sendGet(url);
Map<String, Object> map = parseJson(result);
if (map.containsKey("access_token")) {
accessToken = map.get("access_token").toString();
}
} catch (IOException e) {
e.printStackTrace();
}
return accessToken;
}
/**
* 发送 GET 请求
*/
public static String sendGet(String url) throws IOException {
HttpURLConnection connection = null;
BufferedReader reader = null;
StringBuffer result = new StringBuffer();
try {
URL requestUrl = new URL(url);
connection = (HttpURLConnection) requestUrl.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
if (connection.getResponseCode() == 200) {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
result.append(line);
}
}
} finally {
if (reader != null) {
reader.close();
}
if (connection != null) {
connection.disconnect();
}
}
return result.toString();
}
/**
* 发送 POST 请求
*/
public static String sendPost(String url, String data) {
HttpURLConnection connection = null;
BufferedReader reader = null;
StringBuffer result = new StringBuffer();
try {
URL requestUrl = new URL(url);
connection = (HttpURLConnection) requestUrl.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.connect();
connection.getOutputStream().write(data.getBytes("UTF-8"));
connection.getOutputStream().flush();
connection.getOutputStream().close();
if (connection.getResponseCode() == 200) {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
result.append(line);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection != null) {
connection.disconnect();
}
}
return result.toString();
}
/**
* 解析 JSON 字符串
*/
public static Map<String, Object> parseJson(String jsonStr) {
Map<String, Object> map = new HashMap<>();
try {
JSONObject jsonObject = new JSONObject(jsonStr);
Iterator<String> iterator = jsonObject.keys();
while (iterator.hasNext()) {
String key = iterator.next();
Object value = jsonObject.get(key);
map.put(key, value);
}
} catch (JSONException e) {
e.printStackTrace();
}
return map;
}
}
```
其中,`getAccessToken` 方法用于获取 access_token,`sendPost` 方法用于发送 POST 请求,`parseJson` 方法用于解析 JSON 字符串。在发送推送消息时,需要将 openid 和消息内容填写到 postData 中。注意需要将 postData 进行 JSON 序列化。
阅读全文