Archive

Archive for May, 2022

Car Registration #API available in #Malta

Malta has a population of just over half a million inhabitants, and with the number of cars on the road at 415,000 as of March 2022 (Source CEIC data), meaning that most households have multiple cars, which is not overly surprising as as rather wealthy island nation. Today, we’ve launched our Maltese website https://www.licenseplate.mt/ that provides an API that allows users determine the make / model and VIN of vehicles registered in Malta.

Car registration plates in Malta use the /CheckMalta endpoint and return the following
information:


● Make / Model
● Year
● VIN
● Engine Size
● Engine Number
● Power
● Fuel
● Body Style
● Tonnage
● Number of Seats
● Representative image

Sample Registration Number:
JJA127


Sample Json:

{
  "Description": "PEUGEOT 307",
  "RegistrationYear": "2004",
  "CarMake": {
    "CurrentTextValue": "PEUGEOT"
  },
  "CarModel": {
    "CurrentTextValue": "307"
  },
  "MakeDescription": {
    "CurrentTextValue": "PEUGEOT"
  },
  "ModelDescription": {
    "CurrentTextValue": "307"
  },
  "VehicleIdentificationNumber": "VF33HNFUE4S024189",
  "EngineSize": "1587",
  "Power": "109",
  "FuelType": "PETROL",
  "Body": "HATCHBACK",
  "NumberOfSeats": "4",
  "EngineNumber": "10FX4X2148207",
  "Tonnage": "2",
  "ImageUrl": "http://licenseplate.mt/image.aspx/@UEVVR0VPVCAzMDc="
}
Categories: Uncategorized

Serializing a #CookieContainer in C#

If you are using C# to interact with websites, then a common pattern, is that you may log in with one request, and then perform an action with the logged in user with a second request. When you wish to repeat the action, then you may end up logging in again, even though you could have potentially recycled the session from the first log in, which saves both time, and bandwidth.

In order to do so, you will need to somehow store the cookies to disk, and then reload them for the second request.

Using XmlSerialiser in this case won’t work, since the Cookie object contains a Uri object, which is not serialisable, also JSON serialiser (Newtonsoft.JSON) is problematic, since it’s not precisely a 1:1 representation of the CookieContainer (I’m not exactly sure what it misses, please feel free to add comments below with your experience).

I found, that although marked as “obsolete” by Microsoft, the BinaryFormatter provides a faithful representation of the CookieCollection.

This is some code I wote to serialize a Cookie Collection

private static void StoreCookieCollection(IEnumerable cookies, string cookieFile)
{
	cookieFile = Path.GetTempPath() + cookieFile;
	if (File.Exists(cookieFile)) File.Delete(cookieFile);
	var formatter = new BinaryFormatter();
	var fs = new FileStream(cookieFile, FileMode.Create);
	formatter.Serialize(fs,cookies);
	fs.Close();
}

And the reverse for de-serializing it:

private static CookieCollection LoadCookieCollection(string cookieFile)
{
	cookieFile = Path.GetTempPath() + cookieFile;
	if (!File.Exists(cookieFile)) return null;
	var age = File.GetLastWriteTime(cookieFile);
	if (DateTime.Now - age > TimeSpan.FromDays(1)) return null; // a day old
	var formatter = new BinaryFormatter();
	CookieCollection cc;
	using (Stream reader = new FileStream(cookieFile, FileMode.Open))
	{
		cc = (CookieCollection)formatter.Deserialize(reader);
	}
	return cc;
}

Bon apetit!

Categories: Uncategorized
%d bloggers like this: