如何在delphi中使用Indy 10和OpenSSL通过HTTPS下载/ uplaod文件?

时间:2016-04-19 06:51:26

标签: delphi ssl https sftp indy10

我希望我使用Indy下载文件。 我的问题是连接到SSL。 请帮助我使用HTTPS或SFTP执行此操作。

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  IdFTP1.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(IdFTP1);
  IdFTP1.UseTLS:=utUseRequireTLS;
  IdFTP1.DataPortProtection:=ftpdpsPrivate;
  IdFTP1.Host := '127.0.0.1';
  IdFTP1.Username := 'ftp';
  IdFTP1.Password := '123';
  IdFTP1.Port:=21;
  IdSSLIOHandlerSocketOpenSSL1.StartSSL;
  try
    IdFTP1.Connect;
    IdFTP1.Disconnect;
  finally
    IdFTP1.Free;
  end;
end;

2 个答案:

答案 0 :(得分:0)

您可以正确执行此操作(除非您释放不在同一代码中创建的组件)。 HTTPS也是如此。

IdHTTP1.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create();
IdHTTP1.Get('https://...');

请注意,您需要OpenSSL库 https://indy.fulgan.com/ZIP/SSL.zip 或任何你想要的版本 https://indy.fulgan.com/SSL/

SFTP和FTPS之间也存在差异。 Indy的IdFTP支持SSL(FTPS)。它不能与SFTP(使用SSH的FTP)一起使用。 如果要使用SFTP,则必须使用其他商业组件 https://www.eldos.com/sbb/delphi-sftp.php

如果显示错误,则应指定任何错误。可能只缺少SSL库。 如果您不知道如何下载/上传文件,那么有

的方法
IdFTP1.Put (upload)
IdFTP1.Get (download)

IdHTTP1.Put/Post (upload)
IdHTTP1.Get (download)

您需要来自单元“IdMultipartFormData”的TIdMultiPartFormDataStream才能使HTTP Post上传文件

答案 1 :(得分:0)

嗨,这是一个例子

function HttpGetFile(sUrl, sFile: String) : Boolean;
var
  GetData : TFileStream;
begin
  Result := False;
  try
    GetData := TFileStream.Create(sFile, fmOpenWrite or fmCreate);
    try
        //IdHTTP.ProxyParams.ProxyServer := '';
        //IdHTTP.ProxyParams.ProxyPort := 0;
        //IdHTTP.ProxyParams.ProxyUsername := '';
        //IdHTTP.ProxyParams.ProxyPassword := '';
        //IdHTTP.ProxyParams.BasicAuthentication := False;

        //IdHTTP.ProtocolVersion := pv1_1;
        //IdHTTP.Request.Pragma := 'no-cache';
        //IdHTTP.Request.Connection := 'Keep-Alive';
        //IdHTTP.Request.AcceptLanguage := 'en';
        //IdHTTP.Request.Referer := sTargetUrl;
        //NOTE needs files ssleay32.dll and libeay32.dll
        if (bSecure) and ( FileExists( ExtractFilePath(Application.ExeName) + 'ssleay32.dll')) 
       then begin
          IdSSLIOHandlerSocketOpenSSL.SSLOptions.Method := 
           sslvSSLv23;
          IdSSLIOHandlerSocketOpenSSL.SSLOptions.SSLVersions :=
           [sslvSSLv2,sslvSSLv3,sslvTLSv1,sslvTLSv1_1,sslvTLSv1_2];
          IdHTTP.IOHandler := IdSSLIOHandlerSocketOpenSSL;
          sTargetUrl := HTTPS+UPLOAD_URL;
        end
        else begin
          IdHTTP.IOHandler := nil;
          sTargetUrl := HTTP+UPLOAD_URL;      
        end;
          IdHTTP.Get(sUrl, GetData);

      Result := (IdHTTP.ResponseCode = 200);
    finally
      GetData.Free;
    end;
  except
    on E: EIdOSSLCouldNotLoadSSLLibrary do 
         ShowMessage(E.message);
    on E: EIdHTTPProtocolException do
         ShowMessage(E.message);
    on E: EIdConnClosedGracefully do
         ShowMessage(E.message);
    on E: EIdSocketError do
         ShowMessage(E.message);
    on E: EIdException do
         ShowMessage(E.message);
    on E: Exception do
      ShowMessage(E.message);
  end;
end;