使用摘要式身份验证的 HttpClient post 请求导致错误请求

HttpClient post request with Digest Authentication results in bad request

我正在使用以下代码从 Dahua XVR 摄像机中提取记录并且 returns 记录成功。

        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);

以上代码总是 return 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);