对接海康人脸设备ISAPI,我最开始的时候用Apifox 已经测试成功了
下面是我的实现代码
static async Task PostFace()
{
using (var handler = new HttpClientHandler())
{
//添加权限认证
handler.Credentials = new NetworkCredential("admin", "New202311");
var client = new HttpClient(handler);
client.DefaultRequestHeaders.Add("User-Agent", "Apifox/1.0.0 (https://apifox.com)");
client.DefaultRequestHeaders.Add("Accept", "*/*");
client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
client.DefaultRequestHeaders.Add("Connection", "keep-alive");
//添加表单数据
var parameter = new
{
faceLibType = "blackFD",
FDID = "1",
FPID = "A0087"
};
var faceRecordJsonBytes = JsonConvert.SerializeObject(parameter);
var dataContent = new ByteArrayContent(Encoding.UTF8.GetBytes(faceRecordJsonBytes));
dataContent.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images/face02.jpeg");
var imageData = File.ReadAllBytes(filepath);
var memoryStream = new MemoryStream();
memoryStream.Write(imageData, 0, imageData.Length);
memoryStream.Position = 0; // Rewind the MemoryStream
var imageContent = new ByteArrayContent(imageData);
imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
var boundary = $"-----------------------------{Guid.NewGuid()}";
var form = new MultipartFormDataContent(boundary)
{
{ dataContent,"FaceDataRecord"},
{ imageContent,"FaceImage","face02.jpeg"}
};
HttpResponseMessage responseData =await client.PostAsync("http://111.198.0.160:10011/ISAPI/Intelligent/FDLib/FaceDataRecord?format=json", form);
var body = await responseData.Content.ReadAsStringAsync();
Console.WriteLine(body);
}
}
但是当我用C# 代码实现的时候就一直报错
{
"statusCode": 5,
"statusString": "Invalid Format",
"subStatusCode": "badJsonFormat",
"errorCode": 1342177282,
"errorMsg": "badJsonFormat"
}
URL: http://111.198.0.160:10011/ISAPI/Intelligent/FDLib/FaceDataRecord?format=json
接口使用Digest Auth 授权,认证用户名admin 密码New202311
from body 参数
FaceDataRecord:{"faceLibType":"blackFD","FDID":"1","FPID":"A01902"}
FaceImage :图片二进制
FPID 是工号 测试可以替换成 A01903 、A01904、 A01905 三个
这是我们服务器测试地址 求大佬解决
StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Date: Thu, 28 Dec 2023 15:05:16 GMT
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Connection: keep-alive
Keep-Alive: timeout=5, max=98
Content-Length: 146
Content-Type: application/json
}
是的 400 错误 就是我那个错误 一直不行 妈的 apifox 就能通
@二狗996: 我调不通
参数提交方式问题
能具体点吗兄弟 或者给我个例子
我用postman 试了一下, 也是400错误, 你那个调用成功, 是不是就那么一次成功了...
我每次都可以调用成功,有人测试的时候添加了照片,我已经把照片删除了,你在试试,或者你把FPID 替换成 A01903 、A01904、 A01905
@二狗996:
是很奇怪呢...我代码调用报400 , postman也是报400
@百鸟朝凤: 我刚刚测试了一下 postman 是成功的 ,代码失败是正常的 是很奇怪,真恶心卡我两天了
@二狗996: 我看看你的postman咋写的..我postman都不成功呢,
@百鸟朝凤:
qqq
我也没有搞明白
– echo_lovely 10个月前@echo_lovely: 更奇怪的是 我用抓包工具看了 我代码的请求体和Apifox的请求体一样,不存在JSon 格式的问题。真特马奇怪
– 二狗996 10个月前