How can I take a picture with ISAPI Protocol

sorry before make mistake in code..now success get this return.., but how i can get the image?

1726475660319.png
 
i tried it, but not success, like this

View attachment 203221
This does sound like you are doing digest from a server or toward the server, and that you are hitting CORS POLICY


Cant really tell why it does not work for you. I am using this way for entire integration for all of the hikvision devices, except alarm panels.


You may try this method with simple GET

url ="http://cameraip:cameraport/ISAPI/System/capabilities"


Code:
public class isOnline
{
    public static async Task<bool> PerformOnlineCheck(string url, string username, string password)
    {
        var timeout = TimeSpan.FromSeconds(5);
        Console.WriteLine($"Performing online check for URL: {url} with username: {username}");

        try
        {
            CredentialCache credentialCache = new();
            credentialCache.Add(new Uri(url), "Digest", new NetworkCredential(username, password));

            using var httpClientHandler = new HttpClientHandler { Credentials = credentialCache };
            using var httpClient = new HttpClient(httpClientHandler) { Timeout = timeout };

            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            Console.WriteLine("Sending GET request to the server...");
            var response = await httpClient.GetAsync(url);

            if (response.Content != null)
            {
                string responseContent = await response.Content.ReadAsStringAsync();
                Console.WriteLine($"Response content: {responseContent}");
            }

            Console.WriteLine($"Response status code: {response.StatusCode}");
            return response.IsSuccessStatusCode;
        }
        catch (HttpRequestException ex)
        {
            Console.Error.WriteLine($"HttpRequestException: {ex.Message}");
            if (ex.InnerException != null)
                Console.Error.WriteLine($"Inner Exception: {ex.InnerException.Message}");
            return false;
        }
        catch (TaskCanceledException ex)
        {
            Console.Error.WriteLine("The request was canceled, this usually indicates a timeout.");
            Console.Error.WriteLine($"TaskCanceledException: {ex.Message}");
            return false;
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine($"An error occurred: {ex.Message}");
            return false;
        }
    }
}
 
If you want to save that image somewhere, add path to where you want to save it.

C#:
public class ImageDownloader
{
    public static async Task<bool> SaveImage(string url, string username, string password, string savePath)
    {
        var timeout = TimeSpan.FromSeconds(5);

        try
        {
            CredentialCache credentialCache = new();
            credentialCache.Add(new Uri(url), "Digest", new NetworkCredential(username, password));

            using var httpClientHandler = new HttpClientHandler { Credentials = credentialCache };
            using var httpClient = new HttpClient(httpClientHandler) { Timeout = timeout };

            Console.WriteLine("Sending GET request to the server...");
            var response = await httpClient.GetAsync(url);

            if (response.IsSuccessStatusCode && response.Content != null)
            {
                byte[] imageBytes = await response.Content.ReadAsByteArrayAsync();

                await File.WriteAllBytesAsync(savePath, imageBytes);

                Console.WriteLine($"Image downloaded and saved successfully to {savePath}.");
                return true;
            }
            else
            {
                Console.WriteLine($"Failed to download image. Response status code: {response.StatusCode}");
                return false;
            }
        }
        catch (HttpRequestException ex)
        {
            Console.Error.WriteLine($"HttpRequestException: {ex.Message}");
            if (ex.InnerException != null)
                Console.Error.WriteLine($"Inner Exception: {ex.InnerException.Message}");
            return false;
        }
        catch (TaskCanceledException ex)
        {
            Console.Error.WriteLine("The request was canceled, this usually indicates a timeout.");
            Console.Error.WriteLine($"TaskCanceledException: {ex.Message}");
            return false;
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine($"An error occurred: {ex.Message}");
            return false;
        }
    }
}


And if you want to display it in imagebox rather than saving it, then inner code is


C#:
 using var ms = new MemoryStream(imageBytes);
Image image = Image.FromStream(ms);

pictureBox.Image = image;
 
You do not need to use /r/n to save image, thats only when you use HTTPListener and realtime data from camera which is multipart data.