目前我正在使用以下代码通过TLS / SSL尝试Ftp(上传文件)。对于我正在进行的项目,我有义务/限制使用TLS / SSL w /用户名和密码组合。我也只限于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”。 / p>
编辑#2:我应该通过FileZilla Client和我在FileZilla客户端中为TLS / SSL安全连接设置添加它,我可以将给定的user / pwd组合连接到服务器。所以我猜不是这样。
谢谢, -Y
答案 0 :(得分:0)
遇到了同样的问题,偶然发现了这个问题。在阅读安德鲁·萨维尼赫(Andrew Savinykh)的评论后,我只是添加了端口,它就可以了。希望这对遇到此问题的其他人有所帮助。
例如:
ftpRequest = (FtpWebRequest)FtpWebRequest.Create (ftpPath + ":990" + "/" + remoteFile);