【发布时间】: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