Best way to Save and Retry a URLConnection

I’ve had great success interacting with APIs via URLConnection, and as expected I encounter some routine errors in the transaction (e.g., “504 Gateway Timeout”), and want to retry my request.

At the moment I’m thinking I need to capture all the parameters used to create the request, e.g., RequestHeader(s) and the string used in the original Send method. Then if I get a non-critical error (server busy, etc.) I can put the request back in queue to retry.

Is there an easy way to re-use a URLConnection object, asking it to ‘re-send’?

I don’t bother. I just let the thread terminate straight away if there’s an error (which I log first) and then restart the thread later, from scratch.

This is the best approach, because re-using a URLConnection object has always been unreliable at best. This is where using classes that represent the API rather than forming each request inline comes in handy. Your code interacts with the abstract classes, and it’s the class that generates the URLConnection requests.

You should avoid re-using URLConnection objects. In the past there were bugs that led to data contamination. It’s best to always have a clean connection ready to use.

Thanks @TimStreater and @Tim_Parnell , I’ll continue to create my home-grown creation and queuing (and re-queuing) mechanisms. This works fine - I just didn’t want to overlook any built-in tools that would save me from recreating the wheel. Thanks again.