【发布时间】:2014-03-30 13:26:18
【问题描述】:
目前我正在使用以下代码尝试通过 TLS/SSL 进行 Ftp(上传文件)。对于我正在进行的项目,我有义务/受限于使用用户名和密码组合的 TLS/SSL。我也仅限于 C#/Mono 代码,所以大多数第三方解决方案都不行。
// Setting up variables
FtpWebRequest ftpRequest;
FtpWebResponse ftpResponse = null;
// Configuring & creating the object
ftpRequest = (FtpWebRequest)FtpWebRequest.Create (ftpPath + "/" + remoteFile);
ftpRequest.Credentials = new NetworkCredential ("user", "pwd");
ftpRequest.EnableSsl = true;
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = false;
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
// Hooking up custom validation for certificates (currently this custom method returns "true" in all cases - so this is not a failure point to my knowledge)
ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
// Getting back the ServicePoint object
ServicePoint sp = ftpRequest.ServicePoint;
// Uploading logic
byte[] b = File.ReadAllBytes (localFile);
ftpRequest.ContentLength = b.Length;
using (Stream ftpStream = ftpRequest.GetRequestStream())
{
ftpStream.Write (b, 0, b.Length);
ftpStream.Close ();
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse ();
}
代码(没有 TLS/SSL)完美运行(当然 .EnableSsl = false,我们是通过关闭 TLS/SSL 的 FTP 服务器进行测试)。添加 TLS/SSL 时,我们永远不会超过 using (Stream ftpStream = ftpRequest.GetRequestStream()) 行。抛出以下错误:
System.Net.WebException: Kan geen verbinding met de externe server maken (translated: Unable to connect to the remote server)
at System.Net.FtpWebRequest.CheckError()
at System.Net.FtpWebRequest.GetRequestStream()
at ProjectNameSpace.FtpLogic.FtpUploadFileMethod()
我做错了什么,还有其他解决方案吗?
编辑#1:我确实取回了包含证书信息等的 ServicePoint 对象。代码还流畅地流过 ServerCertificateValidationCallback 方法,在所有情况下我都返回“true”。
编辑#2:我应该通过 FileZilla 客户端添加它,并且在 FileZilla 客户端中设置 TLS/SSL 安全连接,我可以使用给定的用户/密码组合连接到服务器。所以我猜不是这样的。
谢谢, -是的
【问题讨论】:
-
错误提示无法连接到远程服务器。很可能是因为它没有在听。尝试从您的机器远程登录到您认为远程应该侦听以证明/反驳的目标机器上的 ssl 端口。
-
我应该通过 FileZilla 客户端添加,并且在 FileZilla 客户端中设置 TLS/SSL 安全连接,我可以使用给定的用户/密码组合连接到服务器。所以我猜不是这样。
-
对不起,我想说的是某些服务有不同的 SSL 连接端口。显然你的情况不是这样。