具有SSL \ TLS的System.Net.FtpClient - 尝试连接超时

时间:2014-04-15 07:43:45

标签: 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

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

我有这个例外:

  

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

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

2 个答案:

答案 0 :(得分:4)

在设置EncryptionMode

时需要将DataConnectionEncryption设置为true
this.Client.EncryptionMode = FtpEncryptionMode.Explicit;
this.Client.DataConnectionEncryption = true;

答案 1 :(得分:0)

看起来您正在使用FTP活动模式(FtpDataConnectionType.AutoActive)并且FileZilla正在执行被动模式(发送PASV命令,而不是PORT命令)。尝试使用AutoPassive代替AutoActive