System.Net.FtpClient with SSL\TLS - 尝试连接超时

No1*_*ver 1 c# ftp ftp-client ftps

我设置了 FTP 服务器(基于 ubuntu\vsftpd)。

我正在从互联网连接这个 ftp 服务器,所以我在服务器中激活了 SSL 安全功能。

为了检查一切是否正常,我使用 FileZiLla 进行了测试 -> 它正在运行。日志(来自 FileZilla):

Status: Connecting to ****:21...
Status: Connection established, waiting for welcome message...
Response:   220 (vsFTPd 3.0.2)
Command:    AUTH TLS
Response:   234 Proceed with negotiation.
Status: Initializing TLS...
Status: Verifying certificate...
Command:    USER ****
Status: TLS/SSL connection established.
Response:   331 Please specify the password.
Command:    PASS ********
Response:   230 Login successful.
Command:    SYST
Response:   215 UNIX Type: L8
Command:    FEAT
Response:   211-Features:
Response:    AUTH SSL
Response:    AUTH TLS
Response:    EPRT
Response:    EPSV
Response:    MDTM
Response:    PASV
Response:    PBSZ
Response:    PROT
Response:    REST STREAM
Response:    SIZE
Response:    TVFS
Response:    UTF8
Response:   211 End
Command:    OPTS UTF8 ON
Response:   200 Always in UTF8 mode.
Command:    PBSZ 0
Response:   200 PBSZ set to 0.
Command:    PROT P
Response:   200 PROT now Private.
Status: Connected
Status: Retrieving directory listing...
Command:    PWD
Response:   257 "/home/****"
Command:    TYPE I
Response:   200 Switching to Binary mode.
Command:    PASV
Response:   227 Entering Passive Mode (*,*,*,*,*,*).
Command:    LIST
Response:   150 Here comes the directory listing.
Response:   226 Directory send OK.
Status: Directory listing successful
Run Code Online (Sandbox Code Playgroud)

回到我的 C# 代码......我正在尝试使用以下代码连接我的服务器,该代码基于System.Net.FtpClient

public PageDownloader()
        {
            this.Client = new FtpClient();
            this.Client.Host = FTP_HOST;
            this.Client.Port = 21;
            this.Client.Credentials = new NetworkCredential(FTP_USER, FTP_PASS);
            this.Client.DataConnectionType = FtpDataConnectionType.PASV;
            this.Client.EncryptionMode = FtpEncryptionMode.Explicit;
            this.Client.ValidateCertificate += Client_ValidateCertificate;
            this.Client.Connect(); // Exception- System.TimeoutException: Timed out trying to connect!
        }

        void Client_ValidateCertificate(FtpClient control, FtpSslValidationEventArgs e)
        {
            // Never rich here
            e.Accept = true; // Allow all - just for testing...
        }
Run Code Online (Sandbox Code Playgroud)

我有这个例外:

System.TimeoutException:尝试连接超时!在 System.Net.FtpClient.FtpSocketStream.Connect(String host, Int32 port, FtpIpVersion ipVersions) 在 System.Net.FtpClient.FtpClient.Connect()

有人知道为什么会这样吗?为什么我要检查?

Rab*_*olf 5

设置 EncryptionMode 时需要将 DataConnectionEncryption 设置为 true

this.Client.EncryptionMode = FtpEncryptionMode.Explicit;
this.Client.DataConnectionEncryption = true;
Run Code Online (Sandbox Code Playgroud)