作者:ngdongran_638070 | 来源:互联网 | 2023-09-23 09:18
前言QQ小程序内容安全-图片检测https:q.qq.comwikidevelopminiprogramserveropen_portport_safe.html调用接口的代码此处
前言
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;
}