Browse Source

httpclientManger 提取常量;调整超时时间为3-》4秒

dev_shibei_match
jianjun 5 years ago
parent
commit
dd9fadda9c
  1. 40
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/HttpClientManager.java

40
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/HttpClientManager.java

@ -43,7 +43,6 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
@ -60,8 +59,12 @@ import java.util.Set;
*/ */
@Slf4j @Slf4j
public class HttpClientManager { public class HttpClientManager {
private static int connectionTimeout = 3000;// 连接超时时间,毫秒 private static int connectionTimeout = 4000;// 连接超时时间,毫秒
private static int soTimeout = 10000;// 读取数据超时时间,毫秒 private static int soTimeout = 10000;// 读取数据超时时间,毫秒
private static String HEADER_CONTENT_TYPE = "Content-Type";
private static String HEADER_APPLICATION_JSON = "application/json;charset=utf-8";
private static String UTF8 = "utf-8";
private static String HEADER_APPLICATION_FORM_URL_ENCODED = "application/x-www-form-urlencoded;charset=utf-8";
/** /**
* HttpClient对象 * HttpClient对象
*/ */
@ -116,13 +119,14 @@ public class HttpClientManager {
try { try {
HttpPost httppost = new HttpPost(url); HttpPost httppost = new HttpPost(url);
httppost.setConfig(requestConfig); httppost.setConfig(requestConfig);
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded charset=utf-8");
httppost.addHeader(HEADER_CONTENT_TYPE, HEADER_APPLICATION_FORM_URL_ENCODED);
List<NameValuePair> list = new ArrayList<NameValuePair>(); List<NameValuePair> list = new ArrayList<NameValuePair>();
for (String key : paramsMap.keySet()) { for (String key : paramsMap.keySet()) {
list.add(new BasicNameValuePair(key, String.valueOf(paramsMap.get(key)))); list.add(new BasicNameValuePair(key, String.valueOf(paramsMap.get(key))));
} }
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "utf-8"); UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, UTF8);
httppost.setEntity(urlEncodedFormEntity); httppost.setEntity(urlEncodedFormEntity);
return execute(httppost, false); return execute(httppost, false);
@ -146,9 +150,9 @@ public class HttpClientManager {
try { try {
HttpPost httppost = new HttpPost(url); HttpPost httppost = new HttpPost(url);
httppost.setConfig(requestConfig); httppost.setConfig(requestConfig);
httppost.addHeader("Content-Type", "application/json; charset=utf-8"); httppost.addHeader(HEADER_CONTENT_TYPE, HEADER_APPLICATION_JSON);
if (StringUtils.isNotEmpty(jsonStrParam)) { if (StringUtils.isNotEmpty(jsonStrParam)) {
StringEntity se = new StringEntity(jsonStrParam, "utf-8"); StringEntity se = new StringEntity(jsonStrParam, UTF8);
httppost.setEntity(se); httppost.setEntity(se);
} }
return execute(httppost, false); return execute(httppost, false);
@ -164,14 +168,14 @@ public class HttpClientManager {
try { try {
HttpPost httppost = new HttpPost(url); HttpPost httppost = new HttpPost(url);
httppost.setConfig(requestConfig); httppost.setConfig(requestConfig);
httppost.addHeader("Content-Type", "application/json; charset=utf-8"); httppost.addHeader(HEADER_CONTENT_TYPE, HEADER_APPLICATION_JSON);
if (null != headerMap){ if (null != headerMap){
headerMap.forEach((k,v) -> { headerMap.forEach((k,v) -> {
httppost.addHeader(k,v); httppost.addHeader(k,v);
}); });
} }
if (StringUtils.isNotEmpty(jsonStrParam)) { if (StringUtils.isNotEmpty(jsonStrParam)) {
StringEntity se = new StringEntity(jsonStrParam, "utf-8"); StringEntity se = new StringEntity(jsonStrParam, UTF8);
httppost.setEntity(se); httppost.setEntity(se);
} }
return execute(httppost, false); return execute(httppost, false);
@ -221,9 +225,9 @@ public class HttpClientManager {
try { try {
HttpPost httppost = new HttpPost(url); HttpPost httppost = new HttpPost(url);
httppost.setConfig(requestConfig); httppost.setConfig(requestConfig);
httppost.addHeader("Content-Type", "application/json"); httppost.addHeader(HEADER_CONTENT_TYPE, HEADER_APPLICATION_JSON);
if (StringUtils.isNotEmpty(jsonStrParam)) { if (StringUtils.isNotEmpty(jsonStrParam)) {
StringEntity se = new StringEntity(jsonStrParam, "utf-8"); StringEntity se = new StringEntity(jsonStrParam, UTF8);
httppost.setEntity(se); httppost.setEntity(se);
} }
return execute(httppost, true); return execute(httppost, true);
@ -249,9 +253,9 @@ public class HttpClientManager {
try { try {
String stringToSign = timestamp + "\n" + secret; String stringToSign = timestamp + "\n" + secret;
Mac mac = Mac.getInstance("HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256")); mac.init(new SecretKeySpec(secret.getBytes(UTF8), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8")); byte[] signData = mac.doFinal(stringToSign.getBytes(UTF8));
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8"); String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), UTF8);
DingTalkTextMsg msg = new DingTalkTextMsg(); DingTalkTextMsg msg = new DingTalkTextMsg();
msg.setContent(content); msg.setContent(content);
url = url.concat("&timestamp=" + timestamp + "&sign=" + sign); url = url.concat("&timestamp=" + timestamp + "&sign=" + sign);
@ -275,9 +279,9 @@ public class HttpClientManager {
try { try {
String stringToSign = timestamp + "\n" + secret; String stringToSign = timestamp + "\n" + secret;
Mac mac = Mac.getInstance("HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256")); mac.init(new SecretKeySpec(secret.getBytes(UTF8), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8")); byte[] signData = mac.doFinal(stringToSign.getBytes(UTF8));
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8"); String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), UTF8);
log.info("sign为:"+sign); log.info("sign为:"+sign);
url = url.concat("&timestamp=" + timestamp + "&sign=" + sign); url = url.concat("&timestamp=" + timestamp + "&sign=" + sign);
log.info("发送url:"+url); log.info("发送url:"+url);
@ -383,9 +387,9 @@ public class HttpClientManager {
try { try {
HttpPost httppost = new HttpPost(url); HttpPost httppost = new HttpPost(url);
httppost.setConfig(requestConfig); httppost.setConfig(requestConfig);
httppost.addHeader("Content-Type", "application/json"); httppost.addHeader(HEADER_CONTENT_TYPE, HEADER_APPLICATION_JSON);
if (StringUtils.isNotEmpty(json)) { if (StringUtils.isNotEmpty(json)) {
StringEntity se = new StringEntity(json, "utf-8"); StringEntity se = new StringEntity(json, UTF8);
httppost.setEntity(se); httppost.setEntity(se);
} }
return executeToByte(httppost); return executeToByte(httppost);

Loading…
Cancel
Save