我正在使用以下代碼從大華 XVR 攝像機中提取記錄并成功回傳記錄。
var domain = "http://IP";
var credCache = new CredentialCache();
credCache.Add(new Uri(domain), "Digest", new
NetworkCredential(username, password));
var httpClient = new HttpClient(new HttpClientHandler {
Credentials = credCache });
var result= await httpClient.GetStringAsync(new Uri(URL));
但是當我使用以下代碼發布記錄時,它不起作用并導致請求錯誤。
string url = "http://IP/cgi-bin/faceRecognitionServer.cgi";
var postData = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>( "action", "addPerson"),
new KeyValuePair<string, string>("groupID", "1"),
new KeyValuePair<string, string>("name", "Test Name"),
new KeyValuePair<string, string>("birthday", "1980-01-05"),
new KeyValuePair<string, string>("sex", "Male"),
new KeyValuePair<string, string>("country", "Pakistan"),
new KeyValuePair<string, string>("province", "KPK"),
new KeyValuePair<string, string>("city", "Peshawar")
};
var content = new FormUrlEncodedContent(postData);
var domain = "http://IP";
var credCache = new CredentialCache();
credCache.Add(new Uri(domain), "Digest", new NetworkCredential(username, password));
var httpClient = new HttpClient(new HttpClientHandler { Credentials = credCache });
var result = await httpClient.PostAsync(new Uri(url), content);
上面的代碼總是回傳 400 個錯誤的請求。如果有人可以幫忙嗎?
uj5u.com熱心網友回復:
我解決了這個問題,如下所示。也許它可以幫助某人。
減小了我必須嵌入到請求正文中的影像的大小。
將 URL 和引數連接在一個字串中。
string url = "http://IP/cgi-bin/faceRecognitionServer.cgi? action=addPerson&groupID=1&name=TestName&sex=Male"; string domain = "http://IP"; CredentialCache credCache = new CredentialCache { { new Uri(domain), "Digest", new NetworkCredential(username, password) } }; using HttpClient client = new HttpClient(new HttpClientHandler { Credentials = credCache }); using FileStream stream = File.OpenRead(AppContext.BaseDirectory. Replace("\\bin\\Debug\\netcoreapp3.1", "") "Files\\14.jpg"); var file_content = new ByteArrayContent(new StreamContent(stream).ReadAsByteArrayAsync().Result); file_content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); var response = await client.PostAsync(url, file_content);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/343248.html
下一篇:在R中格式化日期
