【问题标题】:'Bad Request' to Face++ FaceDetection API对 Face++ FaceDetection API 的“错误请求”
【发布时间】:2019-02-27 22:02:11
【问题描述】:

我正在使用人脸识别创建应用程序,但我无法调用 Face++ 人脸检测 API。我的请求可能有什么问题?它需要 api 密钥、api 密钥和图像文件作为字节数组或 Base64 字符串。地标和属性是可选的。我将位图发送到函数 AnalyzeFace,它运行良好,直到 client.PostAsync(url, content);。作为响应,它返回

Status code: 400, ReasonPhrase: 'Bad Request'

我的代码

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace WindowsForms
{
    class FaceRecognition
    {
        private const string apiKey = "MY_API_KEY";
        private const string apiSecret = "MY_PRIVATE_KEY";
        private const string attributes = "gender,age";
        private const string landmark = "1";

        private const int CONNECT_TIME_OUT = 30000;

        private static readonly HttpClient client = new HttpClient();
        public static string AnalyzeFace(Bitmap bitmap)
        {
            byte[] image = ImageToByte(bitmap);
            string url = "https://api-us.faceplusplus.com/facepp/v3/detect";
            Dictionary<String,String> dictionary = new Dictionary<string, string>();

            dictionary.Add("api_key", apiKey);
            dictionary.Add("api_secret", apiSecret);
            dictionary.Add("return_landmark", landmark);
            dictionary.Add("image_file",Convert.ToBase64String(image));
            dictionary.Add("return_attributes", attributes);

            try
            {
                CallApi(url,dictionary);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            return null;
        }

        private static async void CallApi(string url, Dictionary<string, string> dictionary)
        {
            var content = new FormUrlEncodedContent(dictionary);

            var response = await client.PostAsync(url, content);

            var responseString = await response.Content.ReadAsStringAsync();
        }

        private static byte[] ImageToByte(Bitmap img)
        {
            ImageConverter converter = new ImageConverter();
            return (byte[])converter.ConvertTo(img, typeof(byte[]));
        }
    }
}

【问题讨论】:

  • 我的猜测是标题之一或提供的数据片段是错误的。使用 Postman 复制您的请求,看看会发生什么。
  • @ThePerplexedOne 伙计,你帮了我很多:D 我又检查了一次标题,发现我使用了错误的键名

标签: c# http httprequest face-recognition


【解决方案1】:

"image_file" 标头用于文件格式的图像。 应该改成"image_base64"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-28
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2017-11-11
    相关资源
    最近更新 更多