【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         }

 

posted @ 2021-10-09 15:53  我有我奥妙  阅读(385)  评论(0编辑  收藏  举报