My brother Rune’s housing association recently started requiring guests to register their guest parking, unless you wanted to get fined. So each time I wanted to visit him, I had to find the link to the registration site, login, register my license plate number, select which lot I would be parking in and select the duration. Since I was registering the same info each time, and my parents were having trouble remembering these steps for their visits too, my developer brain kicked in and asked: “can we automate this?”, to which I replied: “I think so, brain!”.
Sniffing the requests
I started by trying to sniff the HTTPS traffic that was being transmitted when I logged into the site. I used Fiddler, which is great for this purpose. After some fiddling (heh) and checking the “Capture HTTPS traffic” in the Fiddler settings, I was able to find the URL used along with the cookies being sent to the backend API. I was then able to do the same with the remaining two requests: one for validating the license plate and one for the actual parking registration.
Replicating the requests
Next I created a .NET console app to try to perform the requests programmatically. To make my work easier I decided to use Flurl to perform the requests, since adding cookies or headers to HTTP requests are done very easily with this tool. The following gist shows how I used Flurl to perform the authentication request with the supplied username and password I got from the housing association:
The out
parameter from the WithCookies
-method contains the token received from the authentication request and is needed for the following requests. It’s also an example of how easily Flurl lets you retrieve cookies or headers from requests.
Next I had to validate the license plate, which was done like so:
Notice that I’m using the WithCookies
-method again here and passing in the authentication token received from the previous request.
Finally I could do the actual registration request. It required some parameters like which parking lot I was registering the parking for, which I had to find by sniffing the request I was sending from the registration website. In this case I just hardcoded it to parkingGarageId
.
The days
-parameter indicates how many days the parking should be reserved for. Since 2 days was the only option from the registration website I just went for that. guestName
would be my name in this case and the licensePlate
is naturally the license plate for my car. I deserialize the JSON-result into a BookingParkingResult
-class, which just wraps a boolean value which indicates if the registration was successful or not.
Where’s my GUI?
Now I was finally in a state where I could use this inside an app! I could’ve used this in a website as well, but seeing as this would only be used by me and my parents, I decided to create a very simple Xamarin.Forms app which I could privately distribute between us using App Center.
The aforementioned code was refactored into a separate class library, which I could then use in my app.
The app just consists of one page with two buttons: one for registering a parking for my car and one for registering a parking for my parents’ car. Here’s the code-behind for the former:
Notice how I parameterized the name and license plate for the booking request. That way I can easily use the same method when booking for my parents’ car via the other button.
Wrapping up
Flurl is such a great tool for working with HTTP requests. If you haven’t checked it out, I would recommend that you try it out the next time you’re about to create a new HttpClient. If you want to see the code for the Xamarin.Forms app just let me know, but I chose not to include it here since I personally don’t think it brings much value to this post. I just wanted to share how I was able to “reverse engineer” a website to be able to simplify things for both me and my parents.
Nice! Next update lets Rune decide if he wants to accept the guests that are parking should be fined or not.
> “Take that, mom! Next time you’ll save me some dessert…”
Pull requests are welcome! š