java微信消息推送
时间: 2023-11-20 21:49:29 浏览: 167
您可以使用Java的微信开放平台提供的接口来实现微信消息推送功能。下面是一个简单的示例代码:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class WechatPush {
public static void main(String[] args) {
String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=YOUR_ACCESS_TOKEN";
String method = "POST";
String postData = "{\"touser\": \"UserID1|UserID2\",\"toparty\": \"PartyID1|PartyID2\",\"totag\": \"TagID1 | TagID2\",\"msgtype\": \"text\",\"agentid\": 1000002,\"text\": {\"content\": \"Hello World\"},\"safe\": 0}";
try {
URL apiUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.getOutputStream().write(postData.getBytes("UTF-8"));
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
System.out.println(response.toString());
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
请注意,上述代码中的`YOUR_ACCESS_TOKEN`需要替换为您实际的访问令牌,您可以通过微信开放平台的接口获取。
阅读全文