2014-04-15 243 views
1

我做了一個FTP服務器的設置(基於ubuntu \ vsftpd)。帶有SSL TLS的System.Net.FtpClient - 超時嘗試連接

我從互聯網上連接這個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.FtpClient.Connect() System.Net.FtpClient.FtpSocketStream.Connect(字符串主機,端口的Int32, FtpIpVersion ipVersions)

有人知道它爲什麼會發生?爲什麼我應該檢查?

+0

你解決這個問題?與ftps有同樣的問題。 – sangam

回答

0

看起來您正在使用FTP活動模式(FtpDataConnectionType.AutoActive),FileZilla正在做被動模式(發送PASV命令,而不是PORT命令)。嘗試使用AutoPassive而不是AutoActive

+0

已檢查。同樣的問題。 – No1Lives4Ever

+0

您可以添加一些協議跟蹤來查看發生了什麼事情(或使用wireshark捕獲流量,但是您應該使用ftp禁用TLS)。 –

2

需要設置DataConnectionEncryption爲真當您設置了EncryptionMode

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