HttpClient post request with Digest Authentication导致错误请求.<



我使用以下代码从大华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错误请求。有谁能帮忙吗?

我修复的问题如下。也许它能帮助别人。

  1. 减少了我必须嵌入到请求中的图像的大小身体。

  2. 将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);
    

相关内容

  • 没有找到相关文章

最新更新