1

我试图在 ftpwebrequest 中使用 SSL,如下所示

  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。

4

3 回答 3

1

答案很简单,FtpWebRequest 甚至还不够好地支持 FTPS。

引自http://ftps.codeplex.com/

.Net Framework 提供的 System.Net.FTPWebRequest 类非常适合简单的任务(例如下载或上传文件或获取目录列表),并且还通过 EnableSsl 属性支持 SSL 参见:http://blogs.msdn。 com/adarshk/archive/2005/04/22/410925.aspx。那么为什么要为此开设一个新课程呢?

关键是 FTP 中的 SSL 支持不仅仅是一个开/关开关(如在 HTTP/HTTPS 中)。FTP 需要两个单独的连接:一个用于命令(控制连接),一个用于数据(数据连接),用于下载、上传和目录列表。FTPWebRequest.EnableSsl 只是强制在它们两者上使用 SSL。问题是这并不总是合适的。

Microsoft 仅在为 Windows Server 2008 及更高版本提供 FTP 7.5 时才开始在服务器端支持 FTPS。到目前为止,它们甚至在 Internet Explorer 和 Windows Explorer 中都不支持 FTPS。难怪 .NET Framework BCL 缺少 FTPS 支持。

于 2012-02-22T02:37:05.913 回答
0

在这段代码中:new Uri("ftp://" + ftpserverIp + "/" + file)您正在尝试连接到非 SSL ftp 服务器,该服务器用“ftp”表示。将“ftp://”更改为“ftps://”应该会有所帮助(假设服务器支持 SSL。)

于 2012-02-22T00:29:07.407 回答
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,这意味着在当前接受的答案时,这种设置不可能的声明已经是错误的。

于 2014-01-13T20:37:51.317 回答