package com.iailab.sdk.util.http;
|
|
import com.iailab.sdk.auth.client.IailabAuthClient;
|
import com.iailab.sdk.auth.config.AuthProperties;
|
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpResponse;
|
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.entity.StringEntity;
|
import org.apache.http.util.EntityUtils;
|
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.ObjectUtils;
|
|
import java.io.UnsupportedEncodingException;
|
import java.net.URLEncoder;
|
import java.util.Map;
|
|
|
public class IailabHttpUtils {
|
|
private static AuthProperties authProperties;
|
|
public static void setAuthProperties(AuthProperties properties) {
|
authProperties = properties;
|
}
|
|
/**
|
*
|
* @param url
|
* @param map
|
* @param charset
|
* @return
|
*/
|
public static String doGet(String url, Map<String, Object> map, String charset) throws Exception {
|
System.out.println("start doGet url: " + url);
|
checkToken();
|
org.apache.http.client.HttpClient httpClient = null;
|
HttpGet httpGet = null;
|
String result = null;
|
try {
|
StringBuilder sb = new StringBuilder();
|
sb.append(url);
|
if (!CollectionUtils.isEmpty(map)) {
|
if ((url.indexOf("?") == -1)) {
|
sb.append("?");
|
} else {
|
sb.append("&");
|
}
|
map.forEach((k, v) -> {
|
try {
|
sb.append(k + "=" + URLEncoder.encode(v.toString(), charset) + "&");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
});
|
sb.append("t=" + System.currentTimeMillis());
|
}
|
httpClient = HttpClientFactory.getHttpClient();
|
httpGet = new HttpGet(sb.toString());
|
//设置参数
|
httpGet.addHeader("Accept", "application/json");
|
httpGet.addHeader("Content-Type", "application/json;charset=UTF-8");
|
httpGet.addHeader("Authorization", "Bearer " + IailabAuthClient.accessToken);
|
httpGet.addHeader("Tenant-Id", String.valueOf(authProperties.getTenantId()));
|
HttpResponse response = httpClient.execute(httpGet);
|
if (response != null) {
|
HttpEntity resEntity = response.getEntity();
|
if (resEntity != null) {
|
result = EntityUtils.toString(resEntity, charset);
|
}
|
}
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
}
|
return result;
|
}
|
|
/**
|
*
|
* @param url
|
* @param json
|
* @param charset
|
* @return
|
*/
|
public static String doPost(String url, String json, String charset) throws Exception {
|
System.out.println("start doPost url: " + url);
|
checkToken();
|
org.apache.http.client.HttpClient httpClient = null;
|
HttpPost httpPost = null;
|
String result = null;
|
try {
|
httpClient = HttpClientFactory.getHttpClient();
|
httpPost = new HttpPost(url);
|
//设置参数
|
httpPost.addHeader("Accept", "application/json");
|
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
|
httpPost.addHeader("Authorization", "Bearer " + IailabAuthClient.accessToken);
|
httpPost.addHeader("Tenant-Id", String.valueOf(authProperties.getTenantId()));
|
StringEntity stringEntity = new StringEntity(json);
|
stringEntity.setContentEncoding("UTF-8");
|
stringEntity.setContentType("application/json");
|
httpPost.setEntity(stringEntity);
|
HttpResponse response = httpClient.execute(httpPost);
|
if (response != null) {
|
HttpEntity resEntity = response.getEntity();
|
if (resEntity != null) {
|
result = EntityUtils.toString(resEntity, charset);
|
}
|
}
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
}
|
return result;
|
}
|
|
private static void checkToken() throws Exception {
|
//第一次请求或者token过期,需要重新获取token
|
if(ObjectUtils.isEmpty(IailabAuthClient.accessToken)) {
|
IailabAuthClient.authenticate();
|
} else if (IailabAuthClient.expireTime < System.currentTimeMillis() / 1000) {
|
IailabAuthClient.refreshToken();
|
}
|
}
|
|
}
|