2012-02-21 107 views
1

我嘗試使用SSL的FtpWebRequest像下面可能無法連接的FileZilla後啓用SSL中的FtpWebRequest

FileStream outputStream = new FileStream(fileName, FileMode.Append); 
      reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpserverIp + "/" + file)); 
      reqFTP.EnableSsl = true; 
      reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; 
      reqFTP.UseBinary = true; 
      reqFTP.KeepAlive = false; 
      reqFTP.Timeout = -1; 
      reqFTP.UsePassive = true; 
      reqFTP.Credentials = new NetworkCredential("sh", "SE"); 
      FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 
      Stream ftpStream = response.GetResponseStream(); 
      long cl = response.ContentLength; 
      // reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; 
      int bufferSize = 4096; 
      int readCount; 
      byte[] buffer = new byte[bufferSize]; 
      readCount = ftpStream.Read(buffer, 0, bufferSize); 
      Console.WriteLine("Connected: Downloading File"); 
      while (readCount > 0) 
      { 
       outputStream.Write(buffer, 0, readCount); 
       readCount = ftpStream.Read(buffer, 0, bufferSize); 
       //Console.WriteLine(readCount.ToString()); 
      } 

      ftpStream.Close(); 
      outputStream.Close(); 
      response.Close(); 

我這下面的例外:

的遠程證書根據無效驗證程序。

根據我的谷歌刪除私鑰部分的證書中,並且開始處理,現在它拋出

431無法初始化SSL連接

我試着用搜索引擎,但沒有結果,到目前爲止,任何想法的傢伙。

我試圖連接FileZilla。

回答

1

答案很簡單,即FtpWebRequest甚至不支持FTPS。

http://ftps.codeplex.com/

由.NET Framework提供的System.Net.FTPWebRequest類報價,非常適合簡單的任務(例如,下載或上傳文件或得到一個目錄列表),並通過還支持SSL EnableSsl屬性請參閱:http://blogs.msdn.com/adarshk/archive/2005/04/22/410925.aspx。那麼爲什麼要爲這個新班級?

問題是FTP中的SSL支持更多的是開/關切換(如在HTTP/HTTPS中)。 FTP需要兩個單獨的連接:一個用於命令(控制連接)和一個用於數據(數據連接),用於下載,上傳和目錄列表。 FTPWebRequest.EnableSsl只是強制在它們兩個上使用SSL。問題是這並不總是合適的。

當他們爲Windows Server 2008及更高版本提供FTP 7.5時,Microsoft僅在服務器端開始支持FTPS。到目前爲止,他們甚至不支持Internet Explorer中的FTPS和Windows資源管理器。難怪.NET Framework BCL缺乏FTPS支持。

+0

,可否請你糾正我,在這種情況下,我不能使用SSL?如果我想啓用SSL,那麼我怎麼能稱爲ftp服務器(我的意思是URI)?就像ftp? – Usher 2012-02-22 03:27:45

+0

剛剛添加了幾個引號。可以訪問http://blogs.msdn.com/b/adarshk/archive/2005/04/22/410925.aspx查看如何正確處理證書。 – 2012-02-22 05:28:58

0

在這段代碼中:new Uri("ftp://" + ftpserverIp + "/" + file)你試圖連接到一個非SSL的ftp服務器,用「ftp」表示。將「ftp://」改爲「ftps://」應該有幫助(假設服務器支持SSL)。

+0

,我嘗試使用ftps,它直接的方式拋出「的URI前綴無法識別。」 – Usher 2012-02-22 00:46:21

+0

嗯......嗯,有點谷歌搜索帶來了這個:http://forum.filezilla-project.org/viewtopic.php?f=2&t=11545 - 我希望它可以幫助。 – 2012-02-22 00:49:03

+0

再次感謝@Matt T,根據URL提供的建議,改變了我的URL,例如「FTPES://」+ ftpserverIp +「/」+ file)「但是拋出了」URI前綴無法識別「。 – Usher 2012-02-22 00:58:17

0

您缺少的步驟是建立證書驗證策略。

public static bool ValidateCertificate(object sender, 
         X509Certificate certificate, X509Chain chain, 
         SslPolicyErrors sslPolicyErrors) { 
    /** 
    * validation steps for the certificate go here 
    * if you want them, but it may be expedient to 
    * just accept whatever the FTP server gave you. 
    * The channel will be encrypted regardless of 
    * how trusted the certificate is. 
    */ 
    return true; 
} 

,然後設置,當你得到一個證書檢查是撞到了上面的方法:

ServicePointManager.ServerCertificateValidationCallback = 
    new RemoteCertificateValidationCallback(ValidateCertificate); 

給出的例子在工作.NET 3.5

這是用下面的代碼完成意思是在目前接受的答案時,這種設置是不可能的說法已經是錯誤的。