//这里的WeixinUtil.getAccess_token()方法,放在下面。
String aToken = WeixinUtil.getAccess_token("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+你的appId+"&secret="+你的appSecret+"");
System.out.println("这里是aToken"+aToken);
String[] tokenOne = aToken.split(":");
String[] token = tokenOne[1].split(",");
char [] stringArr = token[0].toCharArray();
String token3 = "" ;
for(int i=1;i<stringArr.length-1;i++){
String token2 = String.valueOf(stringArr[i]);
token3 += token2;
}
System.out.println("这里是ACCESS_TOKEN"+token3);
//这里就是一个微信请求,首先用String放着
String tokenurl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+token3;
//首先确定是发送文字消息,还是图文消息,这里是手写的json数据.
//发送文字消息,无连接
String json = "{\"touser\":\"这里是Openid\",\"msgtype\":\"text\",\"text\":{\"content\":\"Hello World\"}}";
//图文消息,有链接连接
String jsonpic = "{\"touser\":\""+这里是Openid+"\","+ "\"msgtype\":\"news\",\"news\":{\"articles\":["+ "{\"title\":\"HelloWorld\",\"url\":\"要跳转的链接"}]}}";
System.out.println("这里是json"+jsonpic);
//请求方法,然后放回OK 成功,否则错误。这里这个请求方法在下边
String xmlStr = HttpKit.post(tokenurl,jsonpic);
System.out.println("这里是xmlStr"+xmlStr);
package com.uitrs.weixin;
import java.net.HttpURLConnection;
import java.net.URL;
public class WeixinUtil {
//传入URL
public static String getAccess_token(String url) {
String accessToken = null;
try {
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet
.openConnection();
http.setRequestMethod("GET"); // 必须是get方式请求
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
accessToken = new String(jsonBytes, "UTF-8");
System.out.println(accessToken);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return accessToken;
}
}
import com.jfinal.kit.HttpKit;
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有