After somebody wrote me an email, about the HttpClient implementation, I'm going to post it here :D. I was originally writing this to integrate with my asp.net core project, so this is created as a CameraService
and used with Dependency injection as Scoped dependecy.
Just for the sake of this answear, I've picked the code, that's usefull universally, however I'll post my DI solution as well.
The only import required is this one:
using System.Net;
Solution:
public class CameraService
{
private readonly string _cameraUrl;
private readonly HttpClient _client;
private readonly static string imgPath = @"C:\Path\To\Final\Image.jpg";
public CameraService(string cameraUrl, string username, string password)
{
// Camera url is going to be something like this: @"http://192.168.0.1XX:80/Streaming/channels/1/picture?snapShotImageType=JPEG"
if (cameraUrl == null) {
throw new Exception("Camera connection url cannot be empty.");
}
if (username == null) {
throw new Exception("Camera username credentials cannot be empty.");
}
if (password == null) {
throw new Exception("Camera password credentials cannot be empty.");
}
_cameraUrl = cameraUrl;
_client = new HttpClient(new HttpClientHandler() {
Credentials = new NetworkCredential(username, password),
Proxy = null
});
}
public async Task GetImage()
{
// Get image from the camera
Stream? responseStream = await _client.GetStreamAsync(_cameraUrl);
if (responseStream == null) {
throw new Exception("Unable to get image from camera.");
}
// Save it into a file
using (FileStream fileStream = new FileStream(imgPath, FileMode.Create, FileAccess.Write, FileShare.Read)) {
responseStream.CopyTo(fileStream);
}
}
}
Asp net core solution:
ICameraService.cs
public interface ICameraService
{
public Task GetImage();
}
CameraService.cs
public class CameraService : ICameraService
{
private readonly string _cameraUrl;
private readonly HttpClient _client;
// Basically just dynamically find where to place the image, could be hardcoded
private readonly static string inputDirForAlpr = Path.Combine(Constants.baseAlprDirPath, "alpr_in");
private readonly static string inputPathForAlprImg = Path.Combine(inputDirForAlpr, "camera_image.jpg");
// Get all login info from asp net core config, can be hardcoded (however I don't recommend it, cuz of security and all that jazz)
public CameraService(ILogger<CameraService> logger, IConfiguration configuration)
{
string? cameraUrl = configuration.GetSection("CameraSettings").GetValue<string>("Url");
string? username = configuration.GetSection("CameraSettings").GetValue<string>("Username");
string? password = configuration.GetSection("CameraSettings").GetValue<string>("Password");
if (cameraUrl == null) {
throw new Exception("Camera connection url cannot be empty.");
}
if (username == null) {
throw new Exception("Camera username credentials cannot be empty.");
}
if (password == null) {
throw new Exception("Camera password credentials cannot be empty.");
}
_cameraUrl = cameraUrl;
_client = new HttpClient(new HttpClientHandler() {
Credentials = new NetworkCredential(username, password),
Proxy = null
});
}
public async Task GetImage()
{
// Cleanup after previous rans
if (Directory.Exists(inputDirForAlpr)) {
Directory.Delete(inputDirForAlpr, true);
}
Directory.CreateDirectory(inputDirForAlpr);
Stream? responseStream = await _client.GetStreamAsync(_cameraUrl);
if (responseStream == null) {
throw new Exception("Unable to get image from camera.");
}
using (FileStream fileStream = new FileStream(inputPathForAlprImg, FileMode.Create, FileAccess.Write, FileShare.Read)) {
responseStream.CopyTo(fileStream);
}
}
}