开发者

ASP.NET Unable to Enable SSL using FtpWebRequest class without getting errors

开发者 https://www.devze.com 2023-03-23 21:08 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号