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);
}
}