提交 | 用户 | 时间
|
cb8c71
|
1 |
package com.iailab.sdk.util.http; |
H |
2 |
|
|
3 |
import com.iailab.sdk.auth.client.IailabAuthClient; |
b233cc
|
4 |
import com.iailab.sdk.auth.config.AuthProperties; |
cb8c71
|
5 |
import org.apache.http.HttpEntity; |
H |
6 |
import org.apache.http.HttpResponse; |
|
7 |
import org.apache.http.client.methods.HttpGet; |
|
8 |
import org.apache.http.client.methods.HttpPost; |
|
9 |
import org.apache.http.entity.StringEntity; |
|
10 |
import org.apache.http.util.EntityUtils; |
|
11 |
import org.springframework.util.CollectionUtils; |
b233cc
|
12 |
import org.springframework.util.ObjectUtils; |
cb8c71
|
13 |
|
H |
14 |
import java.io.UnsupportedEncodingException; |
|
15 |
import java.net.URLEncoder; |
|
16 |
import java.util.Map; |
|
17 |
|
|
18 |
|
|
19 |
public class IailabHttpUtils { |
|
20 |
|
b233cc
|
21 |
private static AuthProperties authProperties; |
H |
22 |
|
|
23 |
public static void setAuthProperties(AuthProperties properties) { |
|
24 |
authProperties = properties; |
|
25 |
} |
|
26 |
|
cb8c71
|
27 |
/** |
H |
28 |
* |
|
29 |
* @param url |
|
30 |
* @param map |
|
31 |
* @param charset |
|
32 |
* @return |
|
33 |
*/ |
c0b68c
|
34 |
public static String doGet(String url, Map<String, Object> map, String charset) throws Exception { |
cb8c71
|
35 |
System.out.println("start doGet url: " + url); |
b233cc
|
36 |
checkToken(); |
cb8c71
|
37 |
org.apache.http.client.HttpClient httpClient = null; |
H |
38 |
HttpGet httpGet = null; |
|
39 |
String result = null; |
|
40 |
try { |
|
41 |
StringBuilder sb = new StringBuilder(); |
|
42 |
sb.append(url); |
|
43 |
if (!CollectionUtils.isEmpty(map)) { |
|
44 |
if ((url.indexOf("?") == -1)) { |
|
45 |
sb.append("?"); |
|
46 |
} else { |
|
47 |
sb.append("&"); |
|
48 |
} |
|
49 |
map.forEach((k, v) -> { |
|
50 |
try { |
c0b68c
|
51 |
sb.append(k + "=" + URLEncoder.encode(v.toString(), charset) + "&"); |
cb8c71
|
52 |
} catch (UnsupportedEncodingException e) { |
H |
53 |
e.printStackTrace(); |
|
54 |
} |
|
55 |
}); |
|
56 |
sb.append("t=" + System.currentTimeMillis()); |
|
57 |
} |
|
58 |
httpClient = HttpClientFactory.getHttpClient(); |
|
59 |
httpGet = new HttpGet(sb.toString()); |
|
60 |
//设置参数 |
|
61 |
httpGet.addHeader("Accept", "application/json"); |
|
62 |
httpGet.addHeader("Content-Type", "application/json;charset=UTF-8"); |
b233cc
|
63 |
httpGet.addHeader("Authorization", "Bearer " + IailabAuthClient.accessToken); |
H |
64 |
httpGet.addHeader("Tenant-Id", String.valueOf(authProperties.getTenantId())); |
cb8c71
|
65 |
HttpResponse response = httpClient.execute(httpGet); |
H |
66 |
if (response != null) { |
|
67 |
HttpEntity resEntity = response.getEntity(); |
|
68 |
if (resEntity != null) { |
|
69 |
result = EntityUtils.toString(resEntity, charset); |
|
70 |
} |
|
71 |
} |
|
72 |
} catch (Exception ex) { |
|
73 |
ex.printStackTrace(); |
|
74 |
} |
|
75 |
return result; |
|
76 |
} |
|
77 |
|
|
78 |
/** |
|
79 |
* |
|
80 |
* @param url |
|
81 |
* @param json |
|
82 |
* @param charset |
|
83 |
* @return |
|
84 |
*/ |
b233cc
|
85 |
public static String doPost(String url, String json, String charset) throws Exception { |
cb8c71
|
86 |
System.out.println("start doPost url: " + url); |
b233cc
|
87 |
checkToken(); |
cb8c71
|
88 |
org.apache.http.client.HttpClient httpClient = null; |
H |
89 |
HttpPost httpPost = null; |
|
90 |
String result = null; |
|
91 |
try { |
|
92 |
httpClient = HttpClientFactory.getHttpClient(); |
|
93 |
httpPost = new HttpPost(url); |
|
94 |
//设置参数 |
|
95 |
httpPost.addHeader("Accept", "application/json"); |
|
96 |
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8"); |
b233cc
|
97 |
httpPost.addHeader("Authorization", "Bearer " + IailabAuthClient.accessToken); |
H |
98 |
httpPost.addHeader("Tenant-Id", String.valueOf(authProperties.getTenantId())); |
cb8c71
|
99 |
StringEntity stringEntity = new StringEntity(json); |
H |
100 |
stringEntity.setContentEncoding("UTF-8"); |
|
101 |
stringEntity.setContentType("application/json"); |
|
102 |
httpPost.setEntity(stringEntity); |
|
103 |
HttpResponse response = httpClient.execute(httpPost); |
|
104 |
if (response != null) { |
|
105 |
HttpEntity resEntity = response.getEntity(); |
|
106 |
if (resEntity != null) { |
|
107 |
result = EntityUtils.toString(resEntity, charset); |
|
108 |
} |
|
109 |
} |
|
110 |
} catch (Exception ex) { |
|
111 |
ex.printStackTrace(); |
|
112 |
} |
|
113 |
return result; |
|
114 |
} |
|
115 |
|
b233cc
|
116 |
private static void checkToken() throws Exception { |
H |
117 |
//第一次请求或者token过期,需要重新获取token |
|
118 |
if(ObjectUtils.isEmpty(IailabAuthClient.accessToken)) { |
|
119 |
IailabAuthClient.authenticate(); |
|
120 |
} else if (IailabAuthClient.expireTime < System.currentTimeMillis() / 1000) { |
|
121 |
IailabAuthClient.refreshToken(); |
|
122 |
} |
|
123 |
} |
|
124 |
|
cb8c71
|
125 |
} |