How to download a file over HTTPS using Indy 10 and OpenSSL?

后端 未结 3 590
悲哀的现实
悲哀的现实 2020-12-25 08:59

I have the following task: download a file using HTTPS and authentication. Indy seems the way to go but for some reason it doesn\'t work so far. I have the following in plac

相关标签:
3条回答
  • 2020-12-25 09:17

    I have seen the same thing. Setting the TIdHTTP.ReadTimeout to zero fixes the problem for me.

    ...
    HTTP.IOHandler := IOHandler;
    HTTP.ReadTimeout := 0;
    
    0 讨论(0)
  • 2020-12-25 09:25

    I finally abandoned Indy and OpenSSL and used WinInet for downloading. This is the code that worked for me:

    function Download(URL, User, Pass, FileName: string): Boolean;
    const
      BufferSize = 1024;
    var
      hSession, hURL: HInternet;
      Buffer: array[1..BufferSize] of Byte;
      BufferLen: DWORD;
      F: File;
    begin
       Result := False;
       hSession := InternetOpen('', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0) ;
    
       // Establish the secure connection
       InternetConnect (
         hSession,
         PChar(FullURL),
         INTERNET_DEFAULT_HTTPS_PORT,
         PChar(User),
         PChar(Pass),
         INTERNET_SERVICE_HTTP,
         0,
         0
       );
    
      try
        hURL := InternetOpenURL(hSession, PChar(URL), nil, 0, 0, 0) ;
        try
          AssignFile(f, FileName);
          Rewrite(f,1);
          try
            repeat
              InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen) ;
              BlockWrite(f, Buffer, BufferLen)
            until BufferLen = 0;
          finally
            CloseFile(f) ;
            Result := True;
          end;
        finally
          InternetCloseHandle(hURL)
        end
      finally
        InternetCloseHandle(hSession)
      end;
    end;
    
    0 讨论(0)
  • 2020-12-25 09:26

    All SSL related functions for secure connections will fail unless some additional libraries are installed properly.

    1.) Download libraries

    2.) unzip and copy both DLLs to your project folder (or somewhere in the PATH of your system)

    with that your code from the question works fine for me.

    --reinhard

    0 讨论(0)
提交回复
热议问题