I have an Indy 10 based REST server that provides data to clients (browsers & desktop clients). The server has its KeepAlive property set to true so as to persist connections.
That does you no good in this situation since the code you showed is not taking advantage of persistent connections.
When a desktop client talks to the server, it creates a new thread and a new instance of IdHTTP as in the example below:
Setting the Request.Connection property to 'keep-alive' is wasted in your example since your HTTP object is being freed, and thus disconnecting, when the request is finished.
Also, UTF-8 is not a valid ContentEncoding.
Also, since you clearly want to support compressed responses, you should assign a TIdZLibCompressorBase-derived component, like TIdCompressorZLib, to the TIdHTTP.Compressor property, and let Indy decompress the data for you. However, if you are going to do it manually, you should be looking at the Response.ContentEncoding property to detect a compressed response, not the Request.AcceptEncoding property. And worse, you are indicating that you support both 'gzip' and 'deflate' compression, but you are not actually checking for 'deflate'-encoded responses, only 'gzip'. They are two separate compressions! All the more reason not to assign anything to Request.AcceptEncoding at all. TIdHTTP handles that for you when a Compressor is assigned.
But, in any case, definitely DO NOT process compressed data as a String! It is binary data, not textual data. Charset handling will corrupt the compressed bytes. Use the overloaded version of TIdHTTP.Get() that fills an output TStream instead, and then decompress that if compressed.
Sometimes I have to call this GET procedure as much as 20 times to fill many screen controls. As you can see, IdHTTP is created/freed in the GET function.
I must confess I opted for this create-free method because of users with mobile devices who can move from one WiFi network to another just by walking around. It ensures that I don't have a fixed connection tunnel like TCP/IP which fails when the user's WiFi access changes.
You do realize that HTTP runs on top of TCP/IP, don't you? And your code would still fail if the user happens to change WiFi in the *middle* of an HTTP retrieval.
IdTCPClient/IdTCPServer combo will require reconnection when user's WiFi changes or even when the server has to be restarted.
So does TIdHTTP/TIdHTTPServer, since HTTP runs on top of TCP/IP.
The downside of this IdHTTP / REST Server method is that it is slower than the IdTCPClient/IdTCPServer combo.
Not really, since TIdHTTP derives from TIdTCPClientCustom, and TIdHTTPServer derives from TIdCustomTCPServer, so you still have all of the same TCP/IP semantics.
My question is this, does this create-free method for IdHTTP make persistent connections useless?
Yes. To really take advantage of persistent connections, you have to keep the TIdHTTP object alive and reuse it for subsequent requests. For instance, you could have a pool of TIdHTTP objects, and have your REST thread pull an available object from the pool, use it as needed, and then put it back in the pool when finished.