资讯详情

钉钉机器人加密消息发送

发送钉钉信息进行测试  import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Maps; import com.joolun.cloud.weixin.common.util.HttpUtil; import org.apache.commons.codec.binary.Base64;  import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.net.URLEncoder; import java.util.Arrays; import java.util.List; import java.util.Map;      public class DingDingSpiderUtil {         public static void main(String[] args) {             sendMsg("ceshi,", Arrays.asList("130000000"));         }     public static   boolean sendMsg(String content, List<String> mobileList) {         try {             String dingUrl = getSign() ;             boolean isAtAll = true;             String reqStr = buildReqStr(content, mobileList);             String result = HttpUtil.postJson(dingUrl, reqStr);             System.out.println("result == "   result);             JSONObject obj = JSONObject.parseObject(result);             Integer errcode = obj.getInteger("errcode");             return "ok".equals(obj.getString("errmsg"));         } catch (Exception e) {             e.printStackTrace();         }         return false;     }          public static String  getSign(){             try {                 String url= "https://oapi.dingtalk.com/robot/send?access_token=XXX";                 String secret="SECXXX";                 Long timestamp = System.currentTimeMillis();                 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");                 return url "&timestamp=" timestamp "&sign=" sign;             }catch (Exception e){                 return "";             }         }     private static String buildReqStr(String content, List<String> mobileList) {         Map<String,String> contentMap = Maps.newHashMap();         contentMap.put("content", content);         Map<String, Object> atMap = Maps.newHashMap();     //1.是否通知所有人         atMap.put("isAtAll", true);         //2.通知具体人的手机号码列表         atMap.put("atMobiles", mobileList);         Map<String, Object> reqMap = Maps.newHashMap();         reqMap.put("msgtype", "text");         reqMap.put("text", contentMap);         reqMap.put("at", atMap);         return JSON.toJSONString(reqMap);     } }  

HTTP测试类

import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern;  import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils;
public static String postJson(String url, String body) {     CloseableHttpClient httpClient = createCustomClient();     CloseableHttpResponse response = null;     String resultString = null;     try {         // 创建Http Post请求         HttpPost httpPost = new HttpPost(url);         httpPost.addHeader("Content-Type", "application/json");          if (body != null) {             httpPost.setEntity(new StringEntity(body, "utf-8"));         }         // 执行http请求         response = httpClient.execute(httpPost);         resultString = EntityUtils.toString(response.getEntity(), "utf-8");     } catch (Exception e) {         e.printStackTrace();     } finally {         try {             if (response != null) {                 response.close();             }         } catch (Exception e) {             e.printStackTrace();         }     }      return resultString; }  public static CloseableHttpClient createCustomClient() {     RequestConfig defaultRequestConfig = RequestConfig.custom()             .setSocketTimeout(120 * 1000)             .setConnectTimeout(120 * 1000)             .setConnectionRequestTimeout(120 * 1000)             .setStaleConnectionCheckEnabled(true)             .build();      return HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build(); }

标签: 贴片电容cl21b103kacl21b223kb陶瓷电容cl21b331kb陶瓷电容cl21b392kb陶瓷电容

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台