Copy files from FTP Server Error: SSL cannot be enabled when using a proxy

Last Reply one month ago By dharmendr

Posted one month ago

How to copy files from FTP Server using TLS/SSL Explicit Encryption in asp.net c#

WHENEVER I HAVE USED THIS CODE AND TRYING TO ENABLESSL=TRUE THEN IT SHOWS ERROR "SSL cannot be enabled when using a proxy"

       string ftp = "ftp://SERVER.COM/"; 
       //FTP Folder name. Leave blank if you want to list files from root folder.
       string ftpFolder = ""; 
       try
       {            
           //Create FTP Request.
           FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder);
           request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
           request.AuthenticationLevel = AuthenticationLevel.None;
           //Enter FTP Server credentials.
           request.Credentials = new NetworkCredential("USERNAME", "PASSWORD");
           request.UsePassive = false;
           request.UseBinary = false;
           request.EnableSsl = true;
           request.KeepAlive = false;
            
 
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
 
 
 
           //Fetch the Response and read it using StreamReader.
           FtpWebResponse response = (FtpWebResponse)request.GetResponse();
           List<string> entries = new List<string>();
           using (StreamReader reader = new StreamReader(response.GetResponseStream()))
           {
               //Read the Response as String and split using New Line character.
               entries = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
           }
           response.Close();
 
           //Create a DataTable.
           DataTable dtFiles = new DataTable();
           dtFiles.Columns.AddRange(new DataColumn[3] { new DataColumn("Name", typeof(string)),
                                                   new DataColumn("Size", typeof(decimal)),
                                                   new DataColumn("Date", typeof(string))});
 
           //Loop and add details of each File to the DataTable.
           foreach (string entry in entries)
           {
               string[] splits = entry.Split(new string[] { " ", }, StringSplitOptions.RemoveEmptyEntries);
 
               //Determine whether entry is for File or Directory.
               bool isFile = splits[0].Substring(0, 1) != "d";
               bool isDirectory = splits[0].Substring(0, 1) == "d";
 
               //If entry is for File, add details to DataTable.
               if (isFile)
               {
                   dtFiles.Rows.Add();
                   dtFiles.Rows[dtFiles.Rows.Count - 1]["Size"] = decimal.Parse(splits[4]) / 1024;
                   dtFiles.Rows[dtFiles.Rows.Count - 1]["Date"] = string.Join(" ", splits[5], splits[6], splits[7]);
                   string name = string.Empty;
                   for (int i = 8; i < splits.Length; i++)
                   {
                       name = string.Join(" ", name, splits[i]);
                   }
                   dtFiles.Rows[dtFiles.Rows.Count - 1]["Name"] = name.Trim();
               }
           }
 
           //Bind the GridView.
           gvFiles.DataSource = dtFiles;
           gvFiles.DataBind();
       }
       catch (WebException ex)
       {
           throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
       }

 

Results 1 - 5 of 6 12
Posted one month ago
Hi @sjdali797,
Please try the following

Copy File from FTP Web Server to specific folder using C# and VB.Net in ASP.Net

It might help you.

Cheers Andrea.

Posted one month ago

Thanks for your reply. But it's not working because i need to copy files from FTP Server using TLS/SSL Explicit Encryption in asp.net c#


Posted one month ago

THANKS FOR YOUR REPLY WHENEVER I HAVE USED THIS CODE AND TRYING TO ENABLESSL=TRUE THEN IT SHOWS ERROR "SSL cannot be enabled when using a proxy"

 string ftp = "ftp://SERVER.COM/";

        //FTP Folder name. Leave blank if you want to list files from root folder.
        string ftpFolder = "";

        try
        {
            
            //Create FTP Request.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder);
            request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            request.AuthenticationLevel = AuthenticationLevel.None;
            //Enter FTP Server credentials.
            request.Credentials = new NetworkCredential("USERNAME", "PASSWORD");
            request.UsePassive = false;
            request.UseBinary = false;
            request.EnableSsl = true;
            request.KeepAlive = false;
            

             ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);



            //Fetch the Response and read it using StreamReader.
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            List<string> entries = new List<string>();
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                //Read the Response as String and split using New Line character.
                entries = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
            }
            response.Close();

            //Create a DataTable.
            DataTable dtFiles = new DataTable();
            dtFiles.Columns.AddRange(new DataColumn[3] { new DataColumn("Name", typeof(string)),
                                                    new DataColumn("Size", typeof(decimal)),
                                                    new DataColumn("Date", typeof(string))});

            //Loop and add details of each File to the DataTable.
            foreach (string entry in entries)
            {
                string[] splits = entry.Split(new string[] { " ", }, StringSplitOptions.RemoveEmptyEntries);

                //Determine whether entry is for File or Directory.
                bool isFile = splits[0].Substring(0, 1) != "d";
                bool isDirectory = splits[0].Substring(0, 1) == "d";

                //If entry is for File, add details to DataTable.
                if (isFile)
                {
                    dtFiles.Rows.Add();
                    dtFiles.Rows[dtFiles.Rows.Count - 1]["Size"] = decimal.Parse(splits[4]) / 1024;
                    dtFiles.Rows[dtFiles.Rows.Count - 1]["Date"] = string.Join(" ", splits[5], splits[6], splits[7]);
                    string name = string.Empty;
                    for (int i = 8; i < splits.Length; i++)
                    {
                        name = string.Join(" ", name, splits[i]);
                    }
                    dtFiles.Rows[dtFiles.Rows.Count - 1]["Name"] = name.Trim();
                }
            }

            //Bind the GridView.
            gvFiles.DataSource = dtFiles;
            gvFiles.DataBind();
        }
        catch (WebException ex)
        {
            throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
        }

 



Posted one month ago

I have tried above Links now new Error is coming "Unable to connect to server"