package com.iailab.module.data.common.utils;
|
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.http.Header;
|
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.stereotype.Component;
|
import org.springframework.util.CollectionUtils;
|
|
import java.io.UnsupportedEncodingException;
|
import java.net.URLEncoder;
|
import java.util.Map;
|
import java.util.Objects;
|
|
/**
|
* @author PanZhibao
|
* @date 2021年06月03日 16:40
|
*/
|
@Slf4j
|
@Component
|
public class HttpsRequest {
|
|
/**
|
* doGet
|
*
|
* @param url
|
* @param map
|
* @param charset
|
* @param token
|
* @return
|
*/
|
public String doGet(String url, Map<String, String> map, String charset, String token) {
|
org.apache.http.client.HttpClient httpClient = null;
|
HttpGet httpGet = null;
|
String result = null;
|
try {
|
httpClient = new SSLClient();
|
StringBuilder sb = new StringBuilder();
|
sb.append(url);
|
if (!CollectionUtils.isEmpty(map)) {
|
sb.append("?");
|
map.forEach((k, v) -> {
|
try {
|
sb.append(k + "=" + URLEncoder.encode(v, charset) + "&");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
});
|
sb.append("t=" + System.currentTimeMillis());
|
}
|
log.info("doGet,url=" + sb.toString());
|
httpGet = new HttpGet(sb.toString());
|
//设置参数
|
httpGet.addHeader("Accept", "application/json");
|
httpGet.addHeader("Content-Type", "application/json;charset=UTF-8");
|
httpGet.addHeader("Authorization", token);
|
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;
|
}
|
|
/**
|
* doGet
|
*
|
* @param url
|
* @param map
|
* @param charset
|
* @param authorization
|
* @return
|
*/
|
public String doGetKIO(String url, Map<String, String> map, String charset, String authorization) {
|
org.apache.http.client.HttpClient httpClient = null;
|
HttpGet httpGet = null;
|
String result = null;
|
try {
|
httpClient = new SSLClient();
|
StringBuilder sb = new StringBuilder();
|
sb.append(url);
|
if (!CollectionUtils.isEmpty(map)) {
|
sb.append("?");
|
map.forEach((k, v) -> {
|
try {
|
sb.append(k + "=" + URLEncoder.encode(v, charset) + "&");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
});
|
sb.append("t=" + System.currentTimeMillis());
|
}
|
log.info("doGet,url=" + sb.toString());
|
httpGet = new HttpGet(sb.toString());
|
//设置参数
|
httpGet.addHeader("Accept", "application/json");
|
httpGet.addHeader("Content-Type", "application/json;charset=UTF-8");
|
httpGet.addHeader("Authorization", authorization);
|
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;
|
}
|
|
/**
|
* doPost
|
*
|
* @param url
|
* @param map
|
* @param json
|
* @param charset
|
* @param token
|
* @return
|
*/
|
public String doPost(String url, Map<String, String> map, String json, String charset, String token) {
|
org.apache.http.client.HttpClient httpClient = null;
|
HttpPost httpPost = null;
|
String result = null;
|
try {
|
httpClient = new SSLClient();
|
StringBuilder sb = new StringBuilder();
|
sb.append(url);
|
if (!CollectionUtils.isEmpty(map)) {
|
sb.append("?");
|
map.forEach((k, v) -> {
|
try {
|
sb.append(k + "=" + URLEncoder.encode(v, charset) + "&");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
});
|
sb.append("t=" + System.currentTimeMillis());
|
}
|
httpPost = new HttpPost(sb.toString());
|
//设置参数
|
httpPost.addHeader("Accept", "application/json");
|
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
|
httpPost.addHeader("token", token);
|
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();
|
log.info("doPost失败,url=" + url);
|
}
|
return result;
|
}
|
|
|
/**
|
* doPost
|
*
|
* @param url
|
* @param map
|
* @param json
|
* @param charset
|
* @param authorization
|
* @return
|
*/
|
public String doPostKIO(String url, Map<String, String> map, String json, String charset, String authorization) {
|
org.apache.http.client.HttpClient httpClient = null;
|
HttpPost httpPost = null;
|
String result = null;
|
try {
|
httpClient = new SSLClient();
|
StringBuilder sb = new StringBuilder();
|
sb.append(url);
|
if (!CollectionUtils.isEmpty(map)) {
|
sb.append("?");
|
map.forEach((k, v) -> {
|
try {
|
sb.append(k + "=" + URLEncoder.encode(v, charset) + "&");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
});
|
sb.append("t=" + System.currentTimeMillis());
|
}
|
httpPost = new HttpPost(sb.toString());
|
//设置参数
|
httpPost.addHeader("Accept", "application/json");
|
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
|
httpPost.addHeader("Authorization", authorization);
|
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;
|
}
|
|
/**
|
* doPost
|
*
|
* @param url
|
* @param map
|
* @param json
|
* @param charset
|
* @param token
|
* @return
|
*/
|
public String doPostAuthorization(String url, Map<String, String> map, String json, String charset, String token) {
|
org.apache.http.client.HttpClient httpClient = null;
|
HttpPost httpPost = null;
|
String result = null;
|
try {
|
httpClient = new SSLClient();
|
StringBuilder sb = new StringBuilder();
|
sb.append(url);
|
if (!CollectionUtils.isEmpty(map)) {
|
sb.append("?");
|
map.forEach((k, v) -> {
|
try {
|
sb.append(k + "=" + URLEncoder.encode(v, charset) + "&");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
});
|
sb.append("t=" + System.currentTimeMillis());
|
}
|
httpPost = new HttpPost(sb.toString());
|
//设置参数
|
httpPost.addHeader("Accept", "application/json");
|
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
|
httpPost.addHeader("Authorization", "Bearer{" + token + "}");
|
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;
|
}
|
|
/**
|
* doPost
|
*
|
* @param url
|
* @param map
|
* @param json
|
* @param charset
|
* @return
|
*/
|
public String doPostToken(String url, Map<String, String> map, String json, String charset) {
|
org.apache.http.client.HttpClient httpClient = null;
|
HttpPost httpPost = null;
|
String result = null;
|
try {
|
httpClient = new SSLClient();
|
StringBuilder sb = new StringBuilder();
|
sb.append(url);
|
if (!CollectionUtils.isEmpty(map)) {
|
sb.append("?");
|
map.forEach((k, v) -> {
|
try {
|
sb.append(k + "=" + URLEncoder.encode(v, charset) + "&");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
});
|
sb.append("t=" + System.currentTimeMillis());
|
}
|
httpPost = new HttpPost(sb.toString());
|
//设置参数
|
httpPost.addHeader("Accept", "application/json");
|
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
|
StringEntity stringEntity = new StringEntity(json);
|
stringEntity.setContentEncoding("UTF-8");
|
stringEntity.setContentType("application/json");
|
httpPost.setEntity(stringEntity);
|
HttpResponse response = httpClient.execute(httpPost);
|
if (response != null) {
|
Header[] resHeader = response.getHeaders("X-Auth-Tkn");
|
if (resHeader != null) {
|
result = resHeader[0].getValue();
|
}
|
}
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
}
|
return result;
|
}
|
|
/**
|
* doGet
|
*
|
* @param url
|
* @param map
|
* @param charset
|
* @param token
|
* @param tMap
|
* @return
|
*/
|
public String doGetSDData(String url, Map<String, String> map, String charset, String token, Map<String, String> tMap) {
|
org.apache.http.client.HttpClient httpClient = null;
|
HttpGet httpGet = null;
|
String result = null;
|
try {
|
httpClient = new SSLClient();
|
StringBuilder sb = new StringBuilder();
|
sb.append(url);
|
if (!CollectionUtils.isEmpty(map)) {
|
sb.append("?");
|
map.forEach((k, v) -> {
|
try {
|
sb.append(k + "=" + URLEncoder.encode(v, charset) + "&");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
});
|
sb.append("t=" + System.currentTimeMillis());
|
}
|
log.info("doGet,url=" + sb.toString());
|
httpGet = new HttpGet(sb.toString());
|
//设置参数
|
httpGet.addHeader("Content-Type", "application/json");
|
httpGet.addHeader("X-Auth-Tkn", token);
|
if(!Objects.isNull(tMap.get("X-Forwarded-OrgSet"))){
|
httpGet.addHeader("X-Forwarded-OrgSet", tMap.get("X-Forwarded-OrgSet"));
|
}
|
if(!Objects.isNull(tMap.get("X-Forwarded-PrId"))){
|
httpGet.addHeader("X-Forwarded-PrId", tMap.get("X-Forwarded-PrId"));
|
}
|
if(!Objects.isNull(tMap.get("X-Request-Id"))){
|
httpGet.addHeader("X-Request-Id", tMap.get("X-Request-Id"));
|
}
|
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;
|
}
|
|
/**
|
* doGet
|
*
|
* @param url
|
* @param map
|
* @param charset
|
* @param token
|
* @return
|
*/
|
public String doGetDeviceList(String url, Map<String, String> map, String charset, String token) {
|
org.apache.http.client.HttpClient httpClient = null;
|
HttpGet httpGet = null;
|
String result = null;
|
try {
|
httpClient = new SSLClient();
|
StringBuilder sb = new StringBuilder();
|
sb.append(url);
|
if (!CollectionUtils.isEmpty(map)) {
|
sb.append("?");
|
map.forEach((k, v) -> {
|
try {
|
sb.append(k + "=" + URLEncoder.encode(v, charset) + "&");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
});
|
sb.append("t=" + System.currentTimeMillis());
|
}
|
log.info("doGet,url=" + sb.toString());
|
httpGet = new HttpGet(sb.toString());
|
//设置参数
|
httpGet.addHeader("Content-Type", "application/json");
|
httpGet.addHeader("X-App-Secret", token);
|
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;
|
}
|
}
|