@ -2,7 +2,6 @@ package com.epmet.commons.tools.utils;
import com.alibaba.fastjson.JSON ;
import com.epmet.commons.tools.dto.form.DingTalkTextMsg ;
import com.epmet.commons.tools.exception.EpmetErrorCode ;
import com.google.common.collect.Lists ;
import org.apache.commons.codec.binary.Base64 ;
import org.apache.commons.lang3.StringUtils ;
@ -13,13 +12,9 @@ import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy ;
import javax.crypto.Mac ;
import javax.crypto.spec.SecretKeySpec ;
import java.io.BufferedReader ;
import java.io.IOException ;
import java.io.InputStreamReader ;
import java.io.PrintWriter ;
import java.net.URL ;
import java.net.URLConnection ;
import java.net.URLEncoder ;
import java.nio.charset.StandardCharsets ;
import java.util.concurrent.ArrayBlockingQueue ;
/ * *
@ -104,7 +99,7 @@ public class DingdingMsgSender {
}
private Thread getThread ( ) {
Thread sendMsgThread = new Thread ( "MsgSender-Thread" ) {
return new Thread ( "MsgSender-Thread" ) {
@Override
public void run ( ) {
while ( running ) {
@ -112,7 +107,6 @@ public class DingdingMsgSender {
}
}
} ;
return sendMsgThread ;
}
@ -139,77 +133,19 @@ public class DingdingMsgSender {
try {
String stringToSign = timestamp + "\n" + param . getSecret ( ) ;
Mac mac = Mac . getInstance ( "HmacSHA256" ) ;
mac . init ( new SecretKeySpec ( param . getSecret ( ) . getBytes ( "UTF-8" ) , "HmacSHA256" ) ) ;
byte [ ] signData = mac . doFinal ( stringToSign . getBytes ( "UTF-8" ) ) ;
mac . init ( new SecretKeySpec ( param . getSecret ( ) . getBytes ( StandardCharsets . UTF_8 ) , "HmacSHA256" ) ) ;
byte [ ] signData = mac . doFinal ( stringToSign . getBytes ( StandardCharsets . UTF_8 ) ) ;
String sign = URLEncoder . encode ( new String ( Base64 . encodeBase64 ( signData ) ) , "UTF-8" ) ;
String url = param . getWebHook ( ) ;
url = url . concat ( "×tamp=" + timestamp + "&sign=" + sign ) ;
String jsonStrParam = param . getMsgContent ( ) ;
result = this . sendPostByJSON ( url , jsonStrParam ) ;
result = HttpClientManager . getInstance ( ) . sendPostByJSON ( url , jsonStrParam ) ;
} catch ( Exception e ) {
logger . warn ( "sendPostByJSON error" , e ) ;
}
return result ;
}
/ * *
* 发送POST 请求
*
* @param url 发送请求的 URL
* @param param 请求参数 , JSON格式
* @return
* /
public Result < String > sendPostByJSON ( String url , String param ) throws IOException {
if ( StringUtils . isEmpty ( url ) | | StringUtils . isEmpty ( param ) ) {
throw new IllegalArgumentException ( "参数不能为空" ) ;
}
PrintWriter out = null ;
BufferedReader in = null ;
String result = "" ;
try {
URL realUrl = new URL ( url ) ;
// 打开和URL之间的连接
URLConnection conn = realUrl . openConnection ( ) ;
// 设置通用的请求属性
conn . setRequestProperty ( "accept" , "*/*" ) ;
conn . setRequestProperty ( "Content-Type" , "application/json" ) ;
conn . setRequestProperty ( "connection" , "Keep-Alive" ) ;
conn . setRequestProperty ( "user-agent" ,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)" ) ;
// 发送POST请求必须设置如下两行
conn . setDoOutput ( true ) ;
conn . setDoInput ( true ) ;
// 获取URLConnection对象对应的输出流
out = new PrintWriter ( conn . getOutputStream ( ) ) ;
// 发送请求参数
out . print ( param ) ;
// flush输出流的缓冲
out . flush ( ) ;
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader (
new InputStreamReader ( conn . getInputStream ( ) ) ) ;
String line ;
while ( ( line = in . readLine ( ) ) ! = null ) {
result + = line ;
}
} catch ( Exception e ) {
logger . warn ( "sendPostByJSON error" , e ) ;
return new Result < String > ( ) . error ( EpmetErrorCode . SERVER_ERROR . getCode ( ) , e . getMessage ( ) ) ;
} finally {
try {
if ( out ! = null ) {
out . close ( ) ;
}
if ( in ! = null ) {
in . close ( ) ;
}
} catch ( IOException ex ) {
}
}
return new Result < String > ( ) . ok ( result ) ;
}
public static void main ( String [ ] args ) {
for ( int i = 0 ; i < 50 ; i + + ) {