在 FTPWebrequest 中启用 SSL 后无法连接 FileZilla
我试图在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案很简单,FtpWebRequest 甚至对 FTPS 的支持还不够好。
引用自 http://ftps.codeplex.com/
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/
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.
在这段代码中:
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.)您缺少的步骤是建立证书验证策略。
这是通过以下代码完成的:
然后设置在获得证书检查时要调用的上述方法:
给定的示例适用于 .NET 3.5,这意味着在当前接受的答案时,此设置不可能的声明是已经错了。
The step you're missing is establishing a policy for certificate validation.
This is done with the following code:
and then setting the above method to be hit when you get a certificate check:
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.