houzhongjian
6 天以前 c0b68c59704f48d39aabc02d7009464c6fc36784
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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();
        }
    }
 
}