2012-02-21 90 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

这是用下面的代码完成意思是在目前接受的答案时,这种设置是不可能的说法已经是错误的。