@ -43,7 +43,6 @@ import java.io.File;
import java.io.IOException ;
import java.io.InputStream ;
import java.net.URLEncoder ;
import java.nio.charset.StandardCharsets ;
import java.security.KeyManagementException ;
import java.security.NoSuchAlgorithmException ;
import java.security.cert.CertificateException ;
@ -60,8 +59,14 @@ import java.util.Set;
* /
@Slf4j
public class HttpClientManager {
private static int connectionTimeout = 3000 ; // 连接超时时间,毫秒
private static int soTimeout = 10000 ; // 读取数据超时时间,毫秒
// 连接超时时间,毫秒
private static int connectionTimeout = 5000 ;
// 读取数据超时时间,毫秒
private static int soTimeout = 20000 ;
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对象
* /
@ -116,13 +121,14 @@ public class HttpClientManager {
try {
HttpPost httppost = new HttpPost ( url ) ;
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 > ( ) ;
for ( String key : paramsMap . keySet ( ) ) {
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 ) ;
return execute ( httppost , false ) ;
@ -146,9 +152,9 @@ public class HttpClientManager {
try {
HttpPost httppost = new HttpPost ( url ) ;
httppost . setConfig ( requestConfig ) ;
httppost . addHeader ( "Content-Type" , "application/json; charset=utf-8" ) ;
httppost . addHeader ( HEADER_CONTENT_TYPE , HEADER_APPLICATION_JSON ) ;
if ( StringUtils . isNotEmpty ( jsonStrParam ) ) {
StringEntity se = new StringEntity ( jsonStrParam , "utf-8" ) ;
StringEntity se = new StringEntity ( jsonStrParam , UTF8 ) ;
httppost . setEntity ( se ) ;
}
return execute ( httppost , false ) ;
@ -164,14 +170,14 @@ public class HttpClientManager {
try {
HttpPost httppost = new HttpPost ( url ) ;
httppost . setConfig ( requestConfig ) ;
httppost . addHeader ( "Content-Type" , "application/json; charset=utf-8" ) ;
httppost . addHeader ( HEADER_CONTENT_TYPE , HEADER_APPLICATION_JSON ) ;
if ( null ! = headerMap ) {
headerMap . forEach ( ( k , v ) - > {
httppost . addHeader ( k , v ) ;
} ) ;
}
if ( StringUtils . isNotEmpty ( jsonStrParam ) ) {
StringEntity se = new StringEntity ( jsonStrParam , "utf-8" ) ;
StringEntity se = new StringEntity ( jsonStrParam , UTF8 ) ;
httppost . setEntity ( se ) ;
}
return execute ( httppost , false ) ;
@ -221,9 +227,9 @@ public class HttpClientManager {
try {
HttpPost httppost = new HttpPost ( url ) ;
httppost . setConfig ( requestConfig ) ;
httppost . addHeader ( "Content-Type" , "application/json" ) ;
httppost . addHeader ( HEADER_CONTENT_TYPE , HEADER_APPLICATION_JSON ) ;
if ( StringUtils . isNotEmpty ( jsonStrParam ) ) {
StringEntity se = new StringEntity ( jsonStrParam , "utf-8" ) ;
StringEntity se = new StringEntity ( jsonStrParam , UTF8 ) ;
httppost . setEntity ( se ) ;
}
return execute ( httppost , true ) ;
@ -249,9 +255,9 @@ public class HttpClientManager {
try {
String stringToSign = timestamp + "\n" + secret ;
Mac mac = Mac . getInstance ( "HmacSHA256" ) ;
mac . init ( new SecretKeySpec ( secret . getBytes ( "UTF-8" ) , "HmacSHA256" ) ) ;
byte [ ] signData = mac . doFinal ( stringToSign . getBytes ( "UTF-8" ) ) ;
String sign = URLEncoder . encode ( new String ( Base64 . encodeBase64 ( signData ) ) , "UTF-8" ) ;
mac . init ( new SecretKeySpec ( secret . getBytes ( UTF8 ) , "HmacSHA256" ) ) ;
byte [ ] signData = mac . doFinal ( stringToSign . getBytes ( UTF8 ) ) ;
String sign = URLEncoder . encode ( new String ( Base64 . encodeBase64 ( signData ) ) , UTF8 ) ;
DingTalkTextMsg msg = new DingTalkTextMsg ( ) ;
msg . setContent ( content ) ;
url = url . concat ( "×tamp=" + timestamp + "&sign=" + sign ) ;
@ -275,9 +281,9 @@ public class HttpClientManager {
try {
String stringToSign = timestamp + "\n" + secret ;
Mac mac = Mac . getInstance ( "HmacSHA256" ) ;
mac . init ( new SecretKeySpec ( secret . getBytes ( "UTF-8" ) , "HmacSHA256" ) ) ;
byte [ ] signData = mac . doFinal ( stringToSign . getBytes ( "UTF-8" ) ) ;
String sign = URLEncoder . encode ( new String ( Base64 . encodeBase64 ( signData ) ) , "UTF-8" ) ;
mac . init ( new SecretKeySpec ( secret . getBytes ( UTF8 ) , "HmacSHA256" ) ) ;
byte [ ] signData = mac . doFinal ( stringToSign . getBytes ( UTF8 ) ) ;
String sign = URLEncoder . encode ( new String ( Base64 . encodeBase64 ( signData ) ) , UTF8 ) ;
log . info ( "sign为:" + sign ) ;
url = url . concat ( "×tamp=" + timestamp + "&sign=" + sign ) ;
log . info ( "发送url:" + url ) ;
@ -383,9 +389,9 @@ public class HttpClientManager {
try {
HttpPost httppost = new HttpPost ( url ) ;
httppost . setConfig ( requestConfig ) ;
httppost . addHeader ( "Content-Type" , "application/json" ) ;
httppost . addHeader ( HEADER_CONTENT_TYPE , HEADER_APPLICATION_JSON ) ;
if ( StringUtils . isNotEmpty ( json ) ) {
StringEntity se = new StringEntity ( json , "utf-8" ) ;
StringEntity se = new StringEntity ( json , UTF8 ) ;
httppost . setEntity ( se ) ;
}
return executeToByte ( httppost ) ;