Skip to main content
 首页 » 编程设计

c#之带 SSL\TLS 的 System.Net.FtpClient之尝试连接超时

2024年02月13日27leader

我搭建了一个FTP服务器(基于ubuntu\vsftpd)。

我正在从 Internet 连接此 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 

回到我的 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... 
        } 

我有这个异常(exception):

System.TimeoutException: Timed out trying to connect! at System.Net.FtpClient.FtpSocketStream.Connect(String host, Int32 port, FtpIpVersion ipVersions) at System.Net.FtpClient.FtpClient.Connect()

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

请您参考如下方法:

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

this.Client.EncryptionMode = FtpEncryptionMode.Explicit; 
this.Client.DataConnectionEncryption = true;