C# 调用imgSecCheck示例/ C#调用微信内容安全接口

原创
2020/03/12 11:50
阅读数 2.6K

一、文字内容检验

普通的pos提交就行,utf-8编码格式

        /// <summary>
        /// 执行内容安全验证
        /// </summary>
        /// <param name="content"></param>
        public bool CheckMsg(string content)
        {
            string url = new LinkManage().GetImg_Sec_Check();
            string data = NetHelper.Post(url, new { content = content }.ToJsonString());
            JObject result = JObject.Parse(data);
            string code = result.Root.SelectToken("errcode").ToString();
            if (code == "87014")
                return false;
            return true;
        }

二、图片检验、图片鉴黄等

需要form提交,提交二进制文件数据

特别说明:content-type 的值:application/octet-stream

1.使用HttpClient

        /// <summary>
        /// 执行内容安全验证
        /// </summary>
        /// <param name="content"></param>
        public bool CheckImg(string imgPath)
        {
            string url = new LinkManage().GetImg_Sec_Check();
            //读取文件内容
            if (File.Exists(imgPath) == false)
                throw new Exception("图片文件不存在");
            byte[] imgData = File.ReadAllBytes(imgPath);

            using (HttpClient client = new HttpClient())
            {
                ByteArrayContent array = new ByteArrayContent(imgData);
                //数据类型,需要对应
                array.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                HttpResponseMessage res = client.PostAsync(url, array).Result;
                string data = res.Content.ReadAsStringAsync().Result;
                JObject result = JObject.Parse(data);
                string code = result.Root.SelectToken("errcode").ToString();
                if (code == "87014")
                    return false;
                return true;
            }
        }

2.使用HttpWebRequest

        /// <summary>
        /// 执行内容安全验证
        /// </summary>
        /// <param name="content"></param>
        public bool CheckImg(string imgPath)
        {
            string url = new LinkManage().GetImg_Sec_Check();
            //读取文件内容
            if (File.Exists(imgPath) == false)
                throw new Exception("图片文件不存在");
            byte[] imgData = File.ReadAllBytes(imgPath);

            Uri uriurl = new Uri(url);
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uriurl);//HttpWebRequest req = (HttpWebRequest)
            req.Method = "Post";
            req.Timeout = 120 * 1000;
            req.ContentType = "application/octet-stream";
            req.ContentLength = imgData.Length;
            using (Stream reqStream = req.GetRequestStream())//using 使用可以释放using段内的内存
            {
                reqStream.Write(imgData, 0, imgData.Length);
                reqStream.Flush();
            }
            try
            {
                using (WebResponse res = req.GetResponse())
                {
                    //在这里对接收到的页面内容进行处理 
                    Stream resStream = res.GetResponseStream();
                    StreamReader resStreamReader = new StreamReader(resStream, System.Text.Encoding.UTF8);
                    string data = resStreamReader.ReadToEnd();
                    resStream.Close();
                    resStreamReader.Close();
                    JObject result = JObject.Parse(data);
                    string code = result.Root.SelectToken("errcode").ToString();
                    if (code == "87014")
                        return false;
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

 

 

更多:

微信小程序判断消息是否已经订阅?订阅消息使用

 微信小程序播放视频卡顿问题

微信小程序文本如何换行

展开阅读全文
打赏
0
0 收藏
分享
加载中
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部