调用微信api推送消息java代码
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
// 发送消息的url地址,根据实际情况修改
String url = "https://api.weixin/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
public void sendMessage(){
try {
URL getUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) getUrl
.openConnection();
connection.connect();
// 发送POST请求必须设置如下两行
connection.setDoOutput(true);
connection.setDoInput(true);
// 获取输出流
OutputStreamWriter out = new OutputStreamWriter(connection .getOutputStream(), "utf-8");
out .append("{\"touser\":\"OPENID\",\"template_id\":\"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY\",\"url\":\"http://weixin./com/index\",\"topcolor\":#FF0000, \"data\":{"name": { "value":"张三", "color":"#173177" },"remark":{ "value":"备注信息", "color":"#173177" }}}"); out .flush(); out .close(); InputStream is = connection .getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line=null ; StringBuffer sb=new StringBuffer() ; while((line=br .readLine())!=null ){ sb .append(line) ;} System .out .println("返回结果:"+sb) ; is .close() ; connection close() ; } catch (Exception e) { e printStackTrace() ; }}
创作工场
免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。
点击这里>>使用🔥专业版,更聪明、更完整、更原创!