C#企业微信机器人发送图片 .net企微机器人发送图片 media md5 not match

  • Post author:
  • Post category:其他


            //读取图片
            string path = Path.Combine(@"c:", DateTime.Now.ToString("yyyyMM") + ".png");
            FileStream fs = new FileStream(path, FileMode.Open);
            MemoryStream memory = new MemoryStream();
            fs.CopyTo(memory);

            //创建MD5
            byte[] bytes= memory.ToArray();
            var result = MD5.Create().ComputeHash(bytes);
            StringBuilder @string= new StringBuilder();
            foreach (var item in result)
            {
                @string.Append(item.ToString("X2"));
            }
            string md5 = @string.ToString().ToUpper();
            memory.Close();
            fs.Close();

            //创建base64
            string base64 = Convert.ToBase64String(bytes);
            //var temp = Regex.Replace(base64, @"[\r\n]", ""); //如果有换行需要去掉换行


            StringBuilder builder = new StringBuilder();
            builder.Append("{");
            builder.Append("\"msgtype\":\"image\",");
            builder.Append("\"image\":{");
            builder.Append("\"base64\":");
            builder.AppendFormat("\"{0}\",", base64);
            builder.AppendFormat("\"md5\":\"{0}\"", md5);
            builder.Append("}");
            builder.Append("}");


            Post("机器人的webhook", builder.ToString());

MD5大小写没关系

创建MD5的时候不要用Stream创建



版权声明:本文为qq_41616234原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。