前言
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; }