HTTPClient.connect_to_host failing if first attempt failed

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Kingdombit

As far as I’m aware, there is no quick way to check for internet access apart from running HTTPClient.connect_to_host and seeing what it returns. But the problem is… if the first attempt to connect to a specific host fails (offline), all attempts after that point also fail when trying to connect to that same host.

Basically, everything works fine if the first time it is run there IS internet access - it will succeed and fail properly for all following attempts depending if the device is online or not… but if it starts with a failure, being offline, it seems to cache that host as unresolvable and not even try anymore even after the internet is turned on.

This trouble also seems to follow over into HTTPRequest, which probably uses HTTPClient.connect_to_host behind the scenes.

I have a particular action that reminds the user to check their internet connection and try again… if they forgot they aren’t online… but this is getting in the way. I’m sure there are some hacky ways to get around it (checking other hosts first before the main one), but this seems like somewhat of a bug… or have no idea what I’m doing, which may very well be possible :slight_smile: Any help would be much appreciated!

:bust_in_silhouette: Reply From: Kingdombit

Alas, I have fallen into the dreaded company of those who end up answering their own question hours later (actually posted the question hours ago, despite what it says). But I wasn’t able to find the question directly answered anywhere else, so this might be helpful for others who end up in the same situation!

The magic is… IP.clear_cache() …just call it after any offline http attempts (HTTPClient or HTTPRequest), and include the hostname (without http:// or https://) to only clear that particular host from the cache, such as IP.clear_cache('')

Helpful for those who want to allow users to do something online after first trying to connect while offline (just remind them to connect, clear cache, and let them try again) :slight_smile: