zoukankan      html  css  js  c++  java
  • 【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;
            }
  • 相关阅读:
    如何打开windows的远程桌面
    mongodb存储引擎
    使用场景举例
    webpack初识
    mongodb快速入门
    mongodb初识
    mysql主从复制与读写分离
    gradle简单配置跟模块依赖
    mysql执行计划
    synchronized的可见性理解
  • 原文地址:https://www.cnblogs.com/masonblog/p/15796026.html
Copyright © 2011-2022 走看看