在 FTPWebrequest 中启用 SSL 后无法连接 FileZilla

发布于 2025-01-08 00:30:41 字数 1567 浏览 1 评论 0原文

我试图在 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。

I was trying to use SSL in ftpwebrequest like below:

  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();

I got this below exception:

The remote certificate is invalid according to the validation procedure.

Based on my google search, I removed the private key part in the certificate and started processing. Now it throws :

431 could not initialize ssl connection

I tried googling but no result so far, any ideas ?

I am trying to connect FileZilla.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扛刀软妹 2025-01-15 00:30:41

答案很简单,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 支持。

The answer is simple that FtpWebRequest does not even support FTPS good enough.

Quoted from http://ftps.codeplex.com/

The System.Net.FTPWebRequest class provided by the .Net Framework, is perfect for simple tasks (e.g. downloading or uploading a file or getting a directory list) and supports also SSL via the EnableSsl property See: http://blogs.msdn.com/adarshk/archive/2005/04/22/410925.aspx . So why a new class for that?

The point is that SSL support in FTP is more that an on/off switch (as in HTTP/HTTPS). FTP requires two separate connections: one for the commands (the control connection) and one for the data (the data connection), for downloads, uploads and directory listings.
FTPWebRequest.EnableSsl simply forces the use of SSL on both of them. The problem is that this is not always suitable.

Microsoft only starts to support FTPS on server side when they provide FTP 7.5 for Windows Server 2008 and above. Up to now, they don't even support FTPS in Internet Explorer nor Windows Explorer. No wonder .NET Framework BCL lacks FTPS support.

澉约 2025-01-15 00:30:41

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

In this segment of code: new Uri("ftp://" + ftpserverIp + "/" + file) you're attempting to connect to a non-SSL ftp server, which is denoted with "ftp". Changing "ftp://" to "ftps://" should help (assuming the server supports SSL.)

雨的味道风的声音 2025-01-15 00:30:41

您缺少的步骤是建立证书验证策略。

这是通过以下代码完成的:

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,这意味着在当前接受的答案时,此设置不可能的声明是已经错了。

The step you're missing is establishing a policy for certificate validation.

This is done with the following code:

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;
}

and then setting the above method to be hit when you get a certificate check:

ServicePointManager.ServerCertificateValidationCallback = 
    new RemoteCertificateValidationCallback(ValidateCertificate);

The given example works in .NET 3.5 meaning at the time of the currently accepted answer, the statement that this setup was impossible was already wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文