钉钉机器人推送消息安全设置三选一(*)
关键字和IP限制都挺简单的。
然后加签官网没有C#的演示代码,只有java和python,在网上找了下C#的
只需将Webhook和加签密钥换了就可以了
[C#] 纯文本查看 复制代码
static void Main(string[] args)
{
string result = "";
//获取毫秒时间戳
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
long shijianchuo = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000);
//获取签名值
string sign = addSign(shijianchuo);
string url = "Webhook×tamp=" + shijianchuo + "&sign=" +sign;
var obj = new
{
msgtype = "text",
text = new
{
content = "钉钉机器人加签测试",
}
};
string json = JsonConvert.SerializeObject(obj);
Console.WriteLine(url);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/json;charset=utf-8";
Console.WriteLine(json);
var bytes = Encoding.UTF8.GetBytes(json);
req.ContentLength = bytes.Length;
using (Stream requestStream = req.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取内容
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
}
Console.WriteLine(result);
Console.ReadKey();
}
/// <summary>
/// 加签
/// </summary>
/// <param name="zTime">当前时间戳</param>
/// <returns></returns>
private static string addSign(long zTime)
{
string secret = "加签密钥";
string stringToSign = zTime + "\n" + secret;
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(secret);
byte[] messageBytes = encoding.GetBytes(stringToSign);
using (var hmacsha256 = new HMACSHA256(keyByte))
{
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
return HttpUtility.UrlEncode(Convert.ToBase64String(hashmessage), Encoding.UTF8);
}
}
蓝奏云:https://www.lanzoui.com/i18zhhqzsvc
钉钉机器人消息推送.zip
(856.67 KB, 下载次数: 59)
|