问题
I try to set a 6 s connection time out for non responding sites, on a TNetHTTPClient with :
NetHTTPClient1.ConnectionTimeout := 6000;
But it doesn't work, the timeout seems always the same default ( about 1 mn) Am I missing something with the ConnectionTimeout or with my code ?
Edit : OK, my mistake, for this tests I used a (bad) proxy and it was the proxy which was not responding. So it seems that the ConnectionTimeout is not efficient in case of not responding proxy. I will post mode code example with the problem.
Edit 2 : So it seems to be more a 'resolve timeout' than a 'connection timeout', the code : First a classic connection all is OK (even if the timeout is not very accurate).
TempTime := Now; //TempTime : TDateTime;
try
NetHTTPClient1.ConnectionTimeout := 2000; // NetHTTPClient1 : TNetHTTPClient;
NetHTTPClient1.Get('http://www.google.com:81');
except
on E: Exception do
showmessage(E.ClassName + ' : ' + E.Message + ', Time : ' + IntToStr(trunc((Now-TempTime)*86400)));
end;
Now a not OK timeout (with a bad proxy) :
NetHTTPClient1.ProxySettings := TProxySettings.Create('157.230.8.180',8080);
TempTime := Now;
try
NetHTTPClient1.ConnectionTimeout := 2000;
NetHTTPClient1.Get('http://www.google.com:81');
except
on E: Exception do
showmessage(E.ClassName + ' : ' + E.Message + ', Time : ' + IntToStr(trunc((Now-TempTime)*86400)));
end;
回答1:
You haven't provided the target platform for your sample code. According to the documentation ConnectionTimeout
property is not supported under OS X
and iOS
. Setting ConnectionTimeout
under those platforms has no effect.
Other than that you might be getting response timeout which occurs after a successful connection to server, but the server fails to serve the request within specified interval.
Unfortunately it's not possible to differentiate between the two by Exception
class, because all you get is general ENetHttpClientException
. At least you can see the difference in the error message - sending
vs receiving
(see below).
Sample code to test (Windows platform): Connection timeout
NetHTTPClient1.ConnectionTimeout := 1000;
NetHTTPClient1.Get('http://www.google.com:81');
{ raises ENetHttpClientException with message 'Error sending data: (12002) The operation timed out'. }
Response timeout
NetHTTPClient1.ResponseTimeout := 1000;
NetHTTPClient1.Get('https://httpstat.us/200?sleep=5000');
{ raises ENetHTTPClientException with message 'Error receiving data: (12002) The operation timed out'. }
There are 4 different kinds of timeouts supported by WinHTTP:
- resolve timeout
- connect timeout
- send timeout
- receive timeout
Current Windows platform implementation of TNetHttpClient
always sets the resolve timeout to 0, connect timeout to ConnectionTimeout
and both send & receive timeout to ResponseTimeout
.
来源:https://stackoverflow.com/questions/56416108/connection-timeout-with-tnethttpclient-and-delphi-10-3