String url = "xxxxx";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Authorization", "xxxxxxxxx");
httpPost.addHeader("Content-Type", "application/json");
JSONObject params = new JSONObject();
params.put("cname", AccessChannel);
params.put("uid", RecordingUID);
params.put("clientRequest", new JSONObject());
httpPost.setEntity(new StringEntity(params.toJSONString()));
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity, CharsetUtils.get(UTF_8));
JSONObject jsonObject = JSONObject.parseObject(result);
return (String) jsonObject.get("resourceId");
- 为了简化,可以写一个类:ImHttpClient单例模式懒加载(http连接池)
- 然后传入请求头的Map,请求体的Map,和url,和timeout时间等等。
- 这样就很简洁。
String url = "xxxxxx";
Map<String, Object> headers = new HashMap<>();
headers.put("Authorization", "xxxx");
JSONObject params = new JSONObject();
params.put("cname", xxx);
params.put("uid", xxxx);
params.put("clientRequest", new JSONObject());
String result = null;
try {
result = ImHttpClient.getHttpClient().postJSONSend(headers, params, url);
JSONObject jsonObject = JSONObject.parseObject(result);
return (String) jsonObject.get("resourceId");
} catch (Exception e) {
log.error("agoraAcquire error {}", ExceptionUtils.getStackTrace(e));
} finally {
log.info("agoraAcquire accessChannel:{} recordingUID:{} url:{} result:{}", accessChannel, recordingUID, url, result);
}
return null;