package com.iailab.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;
|
|
/**
|
* @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)) {
|
if ((url.indexOf("?") == -1)) {
|
sb.append("?");
|
} else {
|
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;
|
}
|
|
/**
|
* 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();
|
}
|
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);
|
httpGet.addHeader("X-Forwarded-OrgSet", tMap.get("X-Forwarded-OrgSet"));
|
httpGet.addHeader("X-Forwarded-PrId", tMap.get("X-Forwarded-PrId"));
|
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;
|
}
|
}
|