| | |
| | | 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.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; |
| | |
| | | |
| | | public class IailabHttpUtils { |
| | | |
| | | private static AuthProperties authProperties; |
| | | |
| | | public static void setAuthProperties(AuthProperties properties) { |
| | | authProperties = properties; |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @param url |
| | |
| | | * @param charset |
| | | * @return |
| | | */ |
| | | public static String doGet(String url, Map<String, String> map, String charset, String accessToken) { |
| | | public static String doGet(String url, Map<String, String> 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; |
| | |
| | | //设置参数 |
| | | httpGet.addHeader("Accept", "application/json"); |
| | | httpGet.addHeader("Content-Type", "application/json;charset=UTF-8"); |
| | | httpGet.addHeader("Authorization", "Bearer " + accessToken); |
| | | httpGet.addHeader("Tenant-Id", String.valueOf(IailabAuthClient.TENANT_ID)); |
| | | 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(); |
| | |
| | | * @param charset |
| | | * @return |
| | | */ |
| | | public static String doPost(String url, String json, String charset, String accessToken) { |
| | | 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; |
| | |
| | | //设置参数 |
| | | httpPost.addHeader("Accept", "application/json"); |
| | | httpPost.addHeader("Content-Type", "application/json;charset=UTF-8"); |
| | | httpPost.addHeader("Authorization", "Bearer " + accessToken); |
| | | httpPost.addHeader("Tenant-Id", String.valueOf(IailabAuthClient.TENANT_ID)); |
| | | 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"); |
| | |
| | | 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(); |
| | | } |
| | | } |
| | | |
| | | } |