private static String obtainTokenResult() throws IOException {
// 创建HttpClient实例
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建post方法实例
HttpPost httppost = new HttpPost(PATH+"/token");
//Request Headers: {“Content-Type”:”application/json”}
//如果headers只有{“Content-Type”:”application/json”},可以不必填写
JSONObject params = new JSONObject();
params.put("grant_type",GRANT_TYPE);
params.put("client_id",CLIENT_ID);
params.put("client_secret",CLIENT_SECRET);
//避免服务端传中文到页面端显示乱码
StringEntity stringEntity = new StringEntity(params.toString(), Charset.forName("UTF-8"));
httppost.setEntity(stringEntity);
CloseableHttpResponse response = httpclient.execute(httppost);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
/**
这里输出和返回不能同时,否则会出现
java.io.IOException: Attempted read from closed stream.
*/
// System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
return EntityUtils.toString(entity, "UTF-8");
}
} finally {
response.close();
}
return null;
}
环信服务端java集成代码示例
最新推荐文章于 2021-03-10 06:58:28 发布