(Indy)TIdHTTP EIdSocketError下载文件时出现套接字错误#0异常

时间:2012-11-05 14:00:18

标签: delphi download delphi-2010 indy

我有这个delphi代码,基本上从安全服务器下载文件(如果我没弄错的话,使用Indy build 10.5.8 r4743):

问题是:我得到随机的“套接字错误#0”异常,我无法解决甚至无法理解:

Project MyProject.exe raised exception class EIdSocketError with message 'Socket Error # 0

Stack is here,pascal代码是:

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;

posted a similar question some time ago,但那是关于上传,而不是下载。从那时起,在SO成员的帮助下修复了代码,现在正在使用相同的代码(用于处理cookie,重新登录等等),所以我认为问题实际上是在显示的下载过程中上述

有人可以看看这个告诉我我做错了吗?

1 个答案:

答案 0 :(得分:2)

就像您的other question一样,如果可能的话,您应该升级到更新的版本,并在请求帮助之前验证问题是否仍然存在。当前版本为 10.5.9 r4861