Cannot upload user to face picture library in Hikvision camera

I’m trying to use the ISAPI to upload a user to the face picture library, but I don’t know where the mistake is. It always responds with an internal server error. Can you help me identify the issue?

below is my code

this is my razor page:

@page "/libraryUser"
@inject FaceLibraryUser FaceLibraryUser
@inject HttpClient Http

<h3>Send Face Data</h3>
<InputFile @ref="fileInput" OnChange="OnFileSelected" />
<button @onclick="SubmitData">Submit</button>

<p>@statusMessage</p>

@code {
    private string statusMessage = "Please select an image.";
    private IBrowserFile selectedFile;
    private string selectedFileName;
    private InputFile fileInput;

    private void OnFileSelected(InputFileChangeEventArgs e)
    {
        selectedFile = e.File;
        if (selectedFile != null)
        {
            selectedFileName = selectedFile.Name;
            statusMessage = $"File selected: {selectedFile.Name}";
        }
        else
        {
            statusMessage = "No file selected.";
        }
    }

    private async Task SubmitData()
    {
        if (selectedFile == null)
        {
            statusMessage = "Please select an image before submitting.";
            return;
        }

        try
        {
            using var stream = selectedFile.OpenReadStream();
            using var memoryStream = new MemoryStream();
            await stream.CopyToAsync(memoryStream);
            var imageBytes = memoryStream.ToArray();

            // Call the function from the FaceLibraryUser service
            var result = await FaceLibraryUser.UploadFaceToLibrary(2, "test", imageBytes, selectedFileName);

            statusMessage = result;
        }
        catch (Exception ex)
        {
            statusMessage = $"Error: {ex.Message}";
        }
    }
}

this is my cs page:

using System.Net;
using System.Net.Http.Headers;
using System.Text;
using System.Xml.Linq;

public class FaceLibraryUser
{
    public static async Task<string> UploadFaceToLibrary(int fdid, string name, byte[] selectedImage, string fileName)
    {
        if (selectedImage == null || selectedImage.Length == 0)
        {
            return "Please select an image file first.";
        }

        var url = "http://192.168.2.64/ISAPI/Intelligent/FDLib/pictureUpload";

        // Prepare the XML data
        var xmlContent = new XElement("PictureUploadData",
            new XElement("FDID", fdid),
            new XElement("FaceAppendData",
                new XElement("name", 1234))
        ).ToString();

        // Convert the XML content to binary format
        var xmlBytes = Encoding.UTF8.GetBytes(xmlContent);
        var xmlBinaryContent = new ByteArrayContent(xmlBytes);
        xmlBinaryContent.Headers.Add("Content-Disposition", "form-data; name="PictureUploadData"");


        // Create content for the image part
        var imageContent = new ByteArrayContent(selectedImage);
        imageContent.Headers.Add("Content-Disposition", $"form-data; name="importImage"; filename="{fileName}"");
        imageContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");


        var boundary = GenerateCustomBoundary();


        // Manually construct the multipart form data
        using var content = new MultipartFormDataContent(boundary);
        content.Add(xmlBinaryContent);
        content.Add(imageContent);

        // Set up HttpClient with HttpClientHandler for Basic Authentication
        var handler = new HttpClientHandler
        {
            Credentials = new NetworkCredential("admin", "iDS-2CD7146G0-IZ") // Replace with your credentials
        };

        using var client = new HttpClient(handler);
        client.DefaultRequestHeaders.Add("Cache-Control", "no-control");
        client.DefaultRequestHeaders.Add("Connection", "keep-alive");
        client.DefaultRequestHeaders.Add("X-Requested-With", "XMLHttpRequest");  // Add X-Requested-With header


        try
        {
            // Send the POST request
            var response = await client.PostAsync(url, content);
            if (response.IsSuccessStatusCode)
            {
                return "User added successfully.";
            }
            else
            {
                var errorResponse = await response.Content.ReadAsStringAsync();
                return $"Failed to add user. Status Code: {response.StatusCode}, Reason: {errorResponse}";
            }
        }
        catch (Exception ex)
        {
            return $"Error: {ex.Message}";
        }
    }


    // Generate custom boundary: 26 dashes followed by 24 digits
    private static string GenerateCustomBoundary()
    {
        var random = new Random();
        var digits = new StringBuilder();

        // Generate 24 random digits
        for (int i = 0; i < 24; i++)
        {
            digits.Append(random.Next(0, 10)); // Random digit between 0 and 9
        }

        return new string('-', 26) + digits.ToString();
    }
}
below is my error message:
Failed to add user. Status Code: InternalServerError, Reason: <?xml version="1.0" encoding="UTF-8"?> <ResponseStatus version="2.0" xmlns="http://www.hikvision.com/ver20/XMLSchema"> <requestURL></requestURL> <statusCode>3</statusCode> <statusString>Device Error</statusString> <subStatusCode>deviceError</subStatusCode> <description>deviceError. The HTTP request is abnormal. Please first confirm whether the device version is supported and whether the request content conforms to the protocol standard, or contact technical support to provide help. Thank you;</description> </ResponseStatus>

error image : errorMessage

i hope can find the error and solution and can success upload user to face picture library

New contributor

ZZZ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật