When working with Binance REST API using WinHTTP, I noticed that requests sometimes are failed with the error
12030 the connection with the server was terminated abnormally
Each request is made like this: (the complete example is here)
WinHttpOpen(...)
WinHttpConnect(...)
{... making request itself; }
if (hRequest) WinHttpCloseHandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
However, despite I close handles, the connection is reused (which I see in the HttpAnalyzer).
I suppose that this is the issue: the server starts dropping connection after N requests or seconds, accoring to server's keep-alive settings; At the same moment my client starts new request which failed with 12030.
Moreover. inspesting successful requests headers, I noticed that
Connection: Close
is passed from the server from time to time; I'd like to reconnect in such cases.
The question is: how do I prevent WInHTTP from trying to reuse connection which is about to be dropped by server?
Changing the registry "TcpTimedWaitDelay" parameter would not be efficient because request count per time may vary.
The same quesstion is actual for Delphi TNetHTTPClient (which uses WinHTTP) and Indy TIdHTTP.
As for Indy, releasing of the TIdHTTP object does not close the connection; however in this case we have source codes. The following patch in the IdSSLOpenSSL.pas
solves it:
If SSL_shutdown(fSSL) = 0
then SSL_shutdown(fSSL);
// SSL_shutdown(fSSL); <= was in original Indy10 IdSSLOpenSSL