Frequently I find a requirement in mobile projects is to consume remote resources, whether it’s images, video, audio etc, the requirement comes up fairly frequently. A common problem I found myself hitting was that when you’re trying to do this with a backend or customisable content that relies on a URL is that sometimes the resource doesn’t exist. This can be because it’s been removed, a typo means it can’t find it or something else.

Mobile network connections are extremly varied from bad underground connections to speedy 5G connections. Being able to handle this can make the difference between users loving an app, or hating it.

To avoid long UI delays with a loading animation and/or text message, or worse just something appearing to freeze, we can do a really quick HTTP call to check a resource exists ahead of actually trying to utilise the resource.

This can also give us some additional information about the resource, such as it’s content type and the content length which lets us prepare the user for any potential lengthy delays.

The technique used here is to use the often overlooked HTTP verb – HEAD. The HEAD request returns quickly whether the resource is there or not by only returning the header of a response and nothing more. If it is, we get valuable information we can use. If it doesn’t, we’re not holding the user up waiting for something and can implement appropriate error handling. This is especially useful if you’re passing a URL to something you don’t have control over (e.g. a video player control or other media component that attempts to load things straight away).

Feel free to look at the sample code below or if you simply want to use the method, it’s in the following NuGet package (along with A LOT more but I haven’t had time to properly document the whole toolkit yet so that’s for another day, and yes it will be open sourced when it’s properly documented too!):

DarkIce.Toolkit.Core

https://www.nuget.org/packages/DarkIce.Toolkit.Core/

So there you go, a simple function that will hopefully create a more pleasant user experience and have your app performing more responsively and pleasing your users.