案列1:获取token
public static String getToken(String requestUrl, String accessKey) {
String access_token = null;
String charset = "utf-8";
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(requestUrl);
httpGet.setHeader("Access-Key", accessKey);
HttpResponse response = httpClient.execute(httpGet);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String result = EntityUtils.toString(resEntity, charset);
JSONObject jsonbody = JSONObject.parseObject(result);
access_token = ((String) jsonbody.get("access_token"));
}
}
} catch (Exception e) {
LOGGER.error("获取Token失败", e);
}
return access_token;
}