Home > Software design >  HttpClient post request with Digest Authentication results in bad request
HttpClient post request with Digest Authentication results in bad request

Time:10-29

I am using the following code to extract records from the Dahua XVR camera and it returns records successfully.

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

but when I am posting records using the following code it's not working and results in a bad request.

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

above code always return 400 bad request. if anyone can help?

CodePudding user response:

I fixed the issue as below. Maybe it help someone.

  1. reduced the size of the image that I had to embed within the request body.

  2. concatenated the URL and parameters in a single string.

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