问题描述
我正在尝试使用 DS-K1T671M Facial Recognition Device from Hikvision
向 Hikvision ISAPI
发送图片。
文档说我需要打电话
POST /ISAPI/Intelligent/FDLib/FaceDataRecord?format=json
body:
{
"faceLibType": "blackFD","FDID": "1","FPID": "1"
}
它说我需要发送这个正文 + 一个二进制图片。因此我需要发送多部分数据(图片+JSON 内容)。
1º 请求
因此,在 Postman 中,如果我在图片中发送此请求。
它给了我一个错误,因为我也没有设置我的 json 正文数据。
2º 请求
然后,我尝试使用 multipart 发送它。
它有文件和 JSON,但仍然没有工作。
来自文档的示例
我将发布文档中的请求
Example
Add Face Record When Binary Picture is Uploaded in Form Format
1) POST /ISAPI/Intelligent/FDLib/FaceDataRecord?format=json
2) Accept: text/html,application/xhtml+xml,3) Accept-Language: us-EN
4) Content-Type: multipart/form-data;boundary=-------------------7e
5) User-Agent: Mozilla/5.0 (cŽmƉĂtibůĞ͖ MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
6) Accept-Encoding: gzip,deflate
7) Host: 10.10.36.29:8080
8) Content-Length: 9907
9) Connection: Keep-Alive
10) Cache-Control: no-cache
11)
12) -----------------------------7e13971310878
13) Content-disposition: form-data; name="FaceDataRecord";
14) Content-Type: application/json
15) Content-Length: 9907
16)
17) {
a) "faceLibType": "blackFD",b) "FDID": "1223344455566788",c) "FPID": "11111aa"
18) }
19) -----------------------------7e13971310878
20) Content-disposition: form-data; name="FaceImage";
21) Content-Type: image/jpeg
22) Content-Length: 9907
23)
24) ......JFIF.....`.`.....C........... .
25) ..
26) ................. $.' ",#..(7),01444.'9=82<.342...C. ....
27) -----------------------------7e13971310878--
他们在 C# 中有一个工作示例
{
szUrl = "/ISAPI/Intelligent/FDLib/FaceDataRecord?format=json";
if (!szUrl.Substring(0,4).Equals("http"))
{
szUrl = "http://" + AddDevice.struDeviceInfo.strDeviceIP + ":" + AddDevice.struDeviceInfo.strHttpPort + szUrl;
}
HttpClient clHttpClient = new HttpClient();
szResponse = string.Empty;
szRequest = "{\"faceLibType\":\"" + comboBoxFaceType.SelectedItem.ToString() +
"\",\"FDID\":\""+textBoxFDID.Text+
"\",\"FPID\":\""+textBoxEmployeeNo.Text+"\"}";
string filePath = textBoxFilePath.Text;
szResponse = clHttpClient.HttpPostData(AddDevice.struDeviceInfo.strUsername,AddDevice.struDeviceInfo.strPassword,szUrl,filePath,szRequest);
ResponseStatus res = JsonConvert.DeserializeObject<ResponseStatus>(szResponse);
if (res!=null && res.statusCode.Equals("1"))
{
MessageBox.Show("Set Picture Succ!");
return;
}
MessageBox.Show("Set Picture Failed!");
}
public string HttpPostData(string strUserName,string strPassword,string url,string filePath,string request)
{
string responseContent = string.Empty;
var memStream = new MemoryStream();
var webRequest = (HttpWebRequest)WebRequest.Create(url);
// 边界符
var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
// 边界符
var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
var fileStream = new FileStream(filePath,FileMode.Open,FileAccess.Read);
// 最后的结束符
var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");
// 设置属性
webRequest.Credentials = GetCredentialCache(url,strUserName,strPassword);
webRequest.Timeout = m_iHttpTimeOut;
webRequest.Method = "POST";
webRequest.Accept = "text/html,";
webRequest.Headers.Add("Accept-Language","zh-CN");
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
webRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
webRequest.Headers.Add("Accept-Encoding","gzip,deflate");
webRequest.Headers.Add("Cache-Control","no-cache");
//写入JSON报文
string header =
"Content-disposition: form-data; name=\"FaceDataRecord\";\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length: "+request.Length+"\r\n\r\n";
var headerBytes = Encoding.UTF8.GetBytes(header);
var requestBytes = Encoding.UTF8.GetBytes(request);
memStream.Write(beginBoundary,beginBoundary.Length);
memStream.Write(headerBytes,headerBytes.Length);
memStream.Write(requestBytes,requestBytes.Length);
var buffer = new byte[fileStream.Length];
fileStream.Read(buffer,buffer.Length);
fileStream.Close();
// 写入文件
header = "\r\n--"+boundary+"\r\n"+
"Content-disposition: form-data; name=\"FaceImage\";\r\n" +
"Content-Type: image/jpeg\r\n"+
"Content-Length: " +buffer.Length+"\r\n\r\n";
headerBytes = Encoding.UTF8.GetBytes(header);
memStream.Write(headerBytes,headerBytes.Length);
memStream.Write(buffer,buffer.Length);
var contentLine = Encoding.ASCII.GetBytes("\r\n");
memStream.Write(contentLine,contentLine.Length);
// 写入最后的结束边界符
memStream.Write(endBoundary,endBoundary.Length);
webRequest.ContentLength = memStream.Length;
var requestStream = webRequest.GetRequestStream();
memStream.Position = 0;
var tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer,tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer,tempBuffer.Length);
requestStream.Close();
try
{
var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(),Encoding.GetEncoding("utf-8")))
{
responseContent = httpStreamReader.ReadToEnd();
}
httpWebResponse.Close();
}
catch(WebException ex)
{
MessageBox.Show(ex.ToString());
}
fileStream.Close();
webRequest.Abort();
return responseContent;
}
解决方法
我认为可以存储的图像的最大大小为 200kb,如相机的网络管理员通知。 所以不能添加更多的图像。 管理页面:http://192.168.1.230/#/home/peopleManage 注意:“图片格式为JPEG,大小小于200K。” 或者您可以查看此问题:
谢谢