import com.alibaba.fastjson.JSONObject;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @Author 鹏
* @Description: 微信专用工具类
*/
public class WeChatUtils {
/***
* appid
*/
private static final String APPID = "sss";
/***
* secret
*/
private static final String SECRET = "ssss";
/**
* 请求方式
*/
private static final String GET = "GET";
private static final String POST = "POST";
/**
* @Author: 鹏
* @Description: 获取公众号的accessToken(可以修改代码改成获取用户的accessToken)
*/
public static JSONObject getAccessToken() throws Exception {
// 获取用户access_token
// String path = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
// path = path.replace("APPID", APPID).replace("SECRET", SECRET).replace("CODE", code);
// 获取公众号获取用户access_token
InputStream inputStream = null;
HttpURLConnection connection = null;
String message = "";
try {
String path = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET";
path = path.replace("APPID", APPID).replace("SECRET", SECRET);
URL url = new URL(path);
connection = (HttpURLConnection) url.openConnection();
//设置本次请求的方式 , 默认是GET方式, 参数要求都是大写字母
connection.setRequestMethod(GET);
//设置连接超时
connection.setConnectTimeout(50000);
//是否打开输入流 , 此方法默认为true
connection.setDoInput(true);
//是否打开输出流, 此方法默认为false
connection.setDoOutput(true);
connection.setUseCaches(false);
//表示连接
connection.connect();
inputStream = connection.getInputStream();
int size = inputStream.available();
byte[] bs = new byte[size];
inputStream.read(bs);
message = new String(bs, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
if (inputStream != null) {
inputStream.close();
}
// 关闭远程链接
if (connection != null) {
connection.disconnect();
}
}
return JSONObject.parseObject(message);
}
/**
* @Author: 鹏
* 文档位置:https://mp.weixin.qq.com/wiki?action=doc&id=mp1421141115&t=0.4126568759642024#62
* 附录1-JS-SDK使用权限签名算法
* @Description: 使用accessToken获取jsapi_ticket
*/
public static JSONObject getJsApiTicket(String accessToken) throws Exception {
InputStream inputStream = null;
HttpURLConnection connection = null;
String message = "";
try {
String path = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
path = path.replace("ACCESS_TOKEN", accessToken);
URL url = new URL(path);
connection = (HttpURLConnection) url.openConnection();
//设置本次请求的方式 , 默认是GET方式, 参数要求都是大写字母
connection.setRequestMethod(GET);
//设置连接超时
connection.setConnectTimeout(50000);
//是否打开输入流 , 此方法默认为true
connection.setDoInput(true);
//是否打开输出流, 此方法默认为false
connection.setDoOutput(true);
connection.setUseCaches(false);
//表示连接
connection.connect();
inputStream = connection.getInputStream();
int size = inputStream.available();
byte[] bs = new byte[size];
inputStream.read(bs);
message = new String(bs, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭资源
if (inputStream != null) {
inputStream.close();
}
// 关闭远程链接
if (connection != null) {
connection.disconnect();
}
}
return JSONObject.parseObject(message);
}
}