【HttpClient】调用QQ小程序安全内容检测接口

前言#

QQ小程序内容安全-图片检测

https://q.qq.com/wiki/develop/miniprogram/server/open_port/port_safe.html

调用接口的代码#

此处仅贴出核心调用的代码,IFormFile对象由Controller中传递,access_token需要自己去获取,HttpClient需要自己去注入

复制代码
        private readonly HttpClient _client;
        public async Task<string> GetQQImgSecCheck(string access_token, IFormFile file)
        {
            string r = string.Empty;

            string url = "https://api.q.qq.com/api/json/security/ImgSecCheck?access_token=" + access_token;
            using (Stream fs = file.OpenReadStream())
            {
                //文件生成的字节数组
                byte[] fileBytes = new byte[fs.Length];
                fs.Read(fileBytes, 0, fileBytes.Length);

                //ByteArray内容,需要重新对Header中的ContentType和ContentDisposition赋值
                var byteFileContent = new ByteArrayContent(fileBytes);
                byteFileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                byteFileContent.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse($"form-data; name=\"media\"; filename=\"{file.FileName}\"");

                //使用MultipartFormDataContent对象,对应为Content-Type = form-data
                MultipartFormDataContent content = new MultipartFormDataContent();
                content.Add(byteFileContent, "media", file.FileName);

                HttpResponseMessage response = await _client.PostAsync(url, content);

                if (response.IsSuccessStatusCode)
                    r = await response.Content.ReadAsStringAsync();
            }

            return r;
        }
复制代码

 

posted @   我有我奥妙  阅读(328)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示
CONTENTS