houzhongjian
2025-03-14 b0430a610332aca416ce8fa735b623650dfdac6c
iailab-plat-sdk/src/main/java/com/iailab/sdk/util/http/IailabHttpUtils.java
@@ -1,7 +1,7 @@
package com.iailab.sdk.util.http;
import com.iailab.sdk.auth.client.IailabAuthClient;
import com.iailab.sdk.auth.config.AuthProperties;
import com.iailab.sdk.auth.client.IailabClient;
import com.iailab.sdk.auth.config.SdkAutoConfiguration;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
@@ -18,12 +18,6 @@
public class IailabHttpUtils {
    private static AuthProperties authProperties;
    public static void setAuthProperties(AuthProperties properties) {
        authProperties = properties;
    }
    /**
     *
     * @param url
@@ -31,7 +25,7 @@
     * @param charset
     * @return
     */
    public static String doGet(String url, Map<String, String> map, String charset) throws Exception {
    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;
@@ -48,7 +42,7 @@
                }
                map.forEach((k, v) -> {
                    try {
                        sb.append(k + "=" + URLEncoder.encode(v, charset) + "&");
                        sb.append(k + "=" + URLEncoder.encode(v.toString(), charset) + "&");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
@@ -60,9 +54,8 @@
            //设置参数
            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()));
            httpGet.addHeader("Authorization", "Bearer " + IailabClient.accessToken);
            httpGet.addHeader("Tenant-Id", SdkAutoConfiguration.TENANT_ID.toString());
            HttpResponse response = httpClient.execute(httpGet);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
@@ -95,8 +88,8 @@
            //设置参数
            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()));
            httpPost.addHeader("Authorization", "Bearer " + IailabClient.accessToken);
            httpPost.addHeader("Tenant-Id", SdkAutoConfiguration.TENANT_ID.toString());
            StringEntity stringEntity = new StringEntity(json);
            stringEntity.setContentEncoding("UTF-8");
            stringEntity.setContentType("application/json");
@@ -116,10 +109,10 @@
    private static void checkToken() throws Exception {
        //第一次请求或者token过期,需要重新获取token
        if(ObjectUtils.isEmpty(IailabAuthClient.accessToken)) {
            IailabAuthClient.authenticate();
        } else if (IailabAuthClient.expireTime < System.currentTimeMillis() / 1000) {
            IailabAuthClient.refreshToken();
        if(ObjectUtils.isEmpty(IailabClient.accessToken)) {
            IailabClient.authenticate();
        } else if (IailabClient.expireTime < System.currentTimeMillis() / 1000) {
            IailabClient.refreshToken();
        }
    }