我有一个 delphi 代码,它基本上使用Indy 10.4.704将文件上传到远程安全服务器:
IdHTTP := TIdHTTP.Create(nil);
try
TheCompressor := TIdCompressorZLib.Create(nil);
TheSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
with IdHTTP do
begin
HTTPOptions := [hoForceEncodeParams];
AllowCookies := True;
HandleRedirects := True;
ProtocolVersion := pv1_1;
IOHandler := TheSSL;
Compressor := TheCompressor;
end; // with
// Get upload resume offset
try
IdHttp.Head('https://www.domain.com/my-file.bin');
if (IdHttp.Response.ResponseCode <> 404) And (IdHttp.Response.ContentLength >= 0) then
StartPos := IdHttp.Response.ContentLength
else
StartPos := 0;
except
StartPos := 0;
end; // try/except
// Upload File
TheFile := TFileStream.Create(FileName, fmOpenRead OR fmShareDenyWrite);
RangeStream := TIdHTTPRangeStream.Create(TheFile, StartPos, -1, True);
try
if (RangeStream.ResponseCode = 206) then
IdHTTP.Post(https://www.domain.com/upload.php', RangeStream);
finally
RangeStream.Free;
end; // try/finally
finally
FreeAndNil(IdHTTP);
end; // try/finally
问题是有时代码会因 Indy 抛出EIdSocketError Socket Error # 0异常而失败(idHTTP.ResponseCode为 -1)
考虑到我糟糕的互联网连接,我启动了一个 EC2 windows 实例并在上面测试了我的代码(windows 实例在云上运行,所以我认为连接不是问题),但我遇到了同样的问题!
错误似乎是随机的,有时上传有效,有时没有。我用 TidLogFile 调试,我能找到的都是这样的:
Stat Connected.
Sent 4/26/2012 4:18:42: POST /app/upload.php...
Sent 4/26/2012 4:18:42: <uploaded_file_data_here>
Stat Disconnected.
任何人都知道是什么导致了这个/如何解决这个问题?
编辑
我将异常追溯到 TIdSSLIOHandlerSocketOpenSSL。我google了很多,看来这不是SSL错误。