ASP.NET Unable to Enable SSL using FtpWebRequest class without getting errors
I am retrieving a file from our FTP server which is then put into memory and passed to the client via HTTPS. I have everything working flawlessly. However, if I turn on SSL, I get the following error:
WebException: The remote server returned an error: (500) Syntax error, command unrecognized.]
System.Net.FtpWebRequest.SyncRequestCallback(Object obj) +330 System.Net.FtpWebRequest.RequestCallback(Object obj) +23 System.Net.CommandStream.InvokeRequestCallb开发者_开发百科ack(Object obj) +17 System.Net.CommandStream.Abort(Exception e) +168 System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage) +454 System.Net.FtpWebRequest.GetResponse() +1398
Here is my code:
FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create(@"ftp://" + serverName + ":21/" + fileName);
ftp.Credentials = new NetworkCredential(userName, password);
ftp.UseBinary = true;
ftp.EnableSsl = true;
ftp.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = null;
response = (FtpWebResponse)ftp.GetResponse();
If I set ftp.EnableSsl to false or just comment it out, it works fine. Any ideas as to what I am doing wrong. Keep in mind, I am on a Windows XP Professional box using the IIS built in FTP Server. I am debugging from IIS and I am running under https://localhost/projectNameHere/default.aspx.
If you trust the server, you can override Validation process to your need.
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
System.Diagnostics.Debug.WriteLine(certificate);
return true;
}
Your connection and data will still be secure. You should not use this code if you don't trust the source, since the source can be impersonator.
精彩评论