This does sound like you are doing digest from a server or toward the server, and that you are hitting CORS POLICY
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;
}
}
}
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;
}
}
}
using var ms = new MemoryStream(imageBytes);
Image image = Image.FromStream(ms);
pictureBox.Image = image;