• 【HttpClient】请求微信内容安全的图片检测接口


    前言

    使用HttpClient去请求微信内容安全的图片检测接口,验证图片的合法性

    微信接口接口文档地址:

    https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.imgSecCheck.html

    请求代码

     1         /// <summary>
     2         /// 调用微信内容安全的图片信息检查
     3         /// </summary>
     4         /// <param name="access_token">微信小程序请求接口access_token</param>
     5         /// <param name="file">文件信息</param>
     6         /// <returns></returns>
     7         public async Task<string> ImgSecCheck(string access_token, Microsoft.AspNetCore.Http.IFormFile file)
     8         {
     9             string url = $"https://api.weixin.qq.com/wxa/img_sec_check?access_token={access_token}";
    10 
    11             HttpResponseMessage response = null;
    12 
    13             using (Stream fs = file.OpenReadStream())
    14             {
    15                 //原文件生成的字节数组
    16                 byte[] fileBytes = new byte[fs.Length];
    17                 fs.Read(fileBytes, 0, fileBytes.Length);
    18 
    19                 var byteFileContent = new ByteArrayContent(fileBytes);
    20                 byteFileContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");//指定Content-Type
    21                 byteFileContent.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse($"form-data; name="fileName"; filename="{file.FileName}"");
    22 
    23                 response = await _client.PostAsync(url, byteFileContent);
    24             }
    25 
    26             string r = string.Empty;
    27 
    28             if (response.IsSuccessStatusCode)
    29                 r = await response.Content.ReadAsStringAsync();
    30 
    31             return r;
    32         }

    错误代码

    下面的方法在调用其他系统的FromForm接口文件的接口时可以使用

    但是在请求微信内容安全的图片接口时就一直提示 {"errcode":47001,"errmsg":"data format error"} 错误

    使用MultipartFormDataContent去传值,修改了Content-Type、Content-Disposition等信息,还是不行,最终采用了上面的方式

     1         /// <summary>
     2         /// 上传文件
     3         /// </summary>
     4         /// <param name="access_token"></param>
     5         /// <param name="file"></param>
     6         /// <returns></returns>
     7         public async Task<MessageModel<string>> UploadFile(string access_token, Microsoft.AspNetCore.Http.IFormFile file)
     8         {
     9             string url = $"https://api.weixin.qq.com/wxa/img_sec_check?access_token={access_token}";
    10 
    11             MessageModel<string> result = new MessageModel<string>(false, "响应失败");
    12 
    13             using (var content = new MultipartFormDataContent())
    14             {
    15                 using (Stream fs = file.OpenReadStream())
    16                 {
    17                     //原文件生成的字节数组
    18                     byte[] fileBytes = new byte[fs.Length];
    19                     fs.Read(fileBytes, 0, fileBytes.Length);
    20 
    21                     content.Add(new ByteArrayContent(fileBytes), "fileName", file.FileName);
    22 
    23                     HttpResponseMessage response = await _client.PostAsync(url, content);
    24 
    25                     string r = string.Empty;
    26 
    27                     if (response.IsSuccessStatusCode)
    28                         r = await response.Content.ReadAsStringAsync();
    29 
    30                     result.Code = 200;
    31                     result.Msg = "上传成功";
    32                     result.Data = r;
    33                 }
    34             }
    35 
    36             return result;
    37         }
  • 相关阅读:
    【内存泄漏】方法三:利用linux的valgrind命令定位内存泄露(Memory Leak)
    【内存泄漏】方法二:利用linux的mtrace命令定位内存泄露(Memory Leak)
    Windows下sqlmap的使用_01
    关于安装和使用BurpSuite及Java环境的配置问题
    Windows下修改环境变量的几种方法
    OPPO VOOC快充原理
    在线代码运行
    Linux在线学习模拟器
    socket设置为非阻塞模式
    pthread_mutexattr_t设置的相关函数及其说明
  • 原文地址:https://www.cnblogs.com/masonblog/p/15386127.html
Copyright © 2020-2023  润新知