我有这个 delphi 代码,它基本上从安全服务器下载文件(如果我没记错的话,使用 Indy build 10.5.8 r4743):
问题是:我收到了随机的“Socket Error # 0”异常,我无法修复甚至无法理解:
Project MyProject.exe raised exception class EIdSocketError with message 'Socket Error # 0
堆栈在这里,帕斯卡代码是:
IdHTTP := TIdHTTP.Create(nil);
TheSSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
TheCompressor := TIdCompressorZLib.Create(nil);
TheCookieManager := TIdCookieManager.Create(IdHTTP);
try SetupWebComponents(IdHTTP, TheSSL, TheCompressor, TheCookieManager); except end;
TheCookieManager.OnNewCookie := MainForm.SyncNewCookie;
// Go!
Stream := TMemoryStream.Create;
try
IsError := False;
try
with IdHTTP do
begin
OnWork := MainForm.IdHTTPWork_Download;
try
try
IsNewFile := (Not FileExists(LocalFile));
if IsNewFile then
TheFile := TFileStream.Create(LocalFile, fmCreate)
else
// File already exist, resume download
TheFile := TFileStream.Create(LocalFile, fmOpenReadWrite);
DoExit := False;
// Use [ Request.Referer ] it to pass VersionGUID to Work event (to record download progress in db)
Request.Referer := VersionGUID;
repeat
// Get resume byte
if IsNewFile then
begin
ResumeByte := 0;
IsNewFile := False;
end
else
ResumeByte := GetResumeByteFromDB();
if FileExists(LocalFile) then
begin
// File already exist, resume download
DoExit := (ResumeByte >= TheFileSize);
TheFile.Seek(ResumeByte, soFromBeginning);
end;
// Save ResumeByte, it will be used to record download progress in db & eventually use it to resume downloads)
IdHTTP.Tag := ResumeByte;
Request.Range := IntToStr(ResumeByte) + '-';
Get(TheURL, TheFile);
IsError := (Not (ResponseCode in [200, 206])) OR (Pos('login', URL.Document) <> 0);
// Break if there's any error (to allow retrying later in a clean fashion)
if IsError then
Break;
until DoExit;
Disconnect;
except
if (ResponseCode = 404) then
begin
// File doesn't exist, get out
Result := False;
Exit;
end;
end; // try/except
finally
FreeAndNil(TheFile);
end; // try/finally
end; // with
except
IsError := True;
end; // try/except
finally
Stream.Free;
end;
前段时间我发布了一个类似的问题,但那是关于上传,而不是下载。从那时起,在 SO 成员的帮助下,代码得到了修复,现在正在使用相同的代码(用于处理 cookie、重新登录等),所以我认为问题确实出在所示的下载过程中多于
有人可以看看这个告诉我我做错了什么吗?