如何在使用SSL时设置Indy中的ConnectTimeout/ReadTimeout?
MCVE:
MCVE:
program mcve;
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}SysUtils, IdHTTP, IdSSLOpenSSL, DateUtils;
var
HTTP : TIdHTTP;
SSL : TIdSSLIOHandlerSocketOpenSSL;
Started : TDateTime;
begin
HTTP := TIdHTTP.Create();
try
HTTP.ReadTimeout := 1000;
HTTP.ConnectTimeout := 2000;
SSL := TIdSSLIOHandlerSocketOpenSSL.Create(HTTP);
SSL.ConnectTimeout := HTTP.ConnectTimeout;
SSL.ReadTimeout := HTTP.ReadTimeout;
SSL.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
HTTP.IOHandler := SSL;
Started := Now;
try
HTTP.Get(ParamStr(1));
except
On E: Exception do WriteLn(E.Message);
end;
Writeln(FormatDateTime('hh:nn:ss', SecondsBetween(Started, Now) / SecsPerDay));
finally
HTTP.Free;
end;
end.
当使用 http
时,ConnectTimeout/ReadTimeout 可以正常工作。只有在使用 https
时才会出现以下问题:
:~$ ./mcve http://x.x.x.x
Read timed out.
00:00:01 <-- Correct.
:~$ ./mcve https://x.x.x.x
Socket Error # 0
00:03:38 <-- NOT Correct / More than SSL.ReadTimeout value.
使用OPM 10.6.2.5494安装的Lazarus 2.0.6 Indy。
注意:在Windows上,使用Delphi与随附的Indy 10.6.2.5366相同的代码,结果符合预期。
var time : timeval; begin time.tv_sec := 5000; time.tv_usec := 0; fpsetsockopt(ASender.Binding.Handle, Id_SOL_SOCKET, Id_SO_RCVTIMEO, @time, SizeOf(time)); fpsetsockopt(ASender.Binding.Handle, Id_SOL_SOCKET, Id_SO_SNDTIMEO, @time, SizeOf(time)); end;``` 但是没有任何改变。
- RepeatUntil