java怎么实现获取微信access
1个回答
展开全部
/**
* 获得ACCESS_TOKEN
*
* @Title: getAccess_token
* @Description: 获得ACCESS_TOKEN
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public String getAccess_token() {
String url = 'IP="
+ Constants.APP_ID+ "&secret=" + Constants.APPSECRET;
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);
String message = new String(jsonBytes, "UTF-8");
JSONObject demoJson = new JSONObject(message);
accessToken = demoJson.getString("access_token");
System.out.println(accessToken);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return accessToken;
}
转载,仅供参考。
* 获得ACCESS_TOKEN
*
* @Title: getAccess_token
* @Description: 获得ACCESS_TOKEN
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public String getAccess_token() {
String url = 'IP="
+ Constants.APP_ID+ "&secret=" + Constants.APPSECRET;
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);
String message = new String(jsonBytes, "UTF-8");
JSONObject demoJson = new JSONObject(message);
accessToken = demoJson.getString("access_token");
System.out.println(accessToken);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return accessToken;
}
转载,仅供参考。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询