0

I am trying to access an FTP server via a console application written in c#. My application has a time-out at FtpWebRequest.GetResponse() -- the first line inside the try/catch.

I can access the FTP server via Filezilla with the credentials, and the uri + port, supplied to me.

  • I have double checked my credentials with my network admin.
  • I have tried running my code with and without specifying the port in the URI.
  • I have tried adjusting the FtpWebRequest.Timeout value.
  • I have tried using FtpWebRequest.ListDirectory, FtpWebRequest.ListDirectoryDetails, and FtpWebRequest.DownloadFile.
  • I have tried with and without FtpWebRequest.UsePassive = false.

    class Program
    {
        static void Main(string[] args)
        {
            //Uri uri = new Uri(@"ftp://1.2.3.4:990/directory/file.csv");
            Uri uri = new Uri(@"ftp://1.2.3.4:990/directory");
    
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
            ftpRequest.Credentials = new NetworkCredential("username", "password");
            //ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            //ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory
            ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            //ftpRequest.UsePassive = false;
            ftpRequest.Timeout = Convert.ToInt32(0.5d * 60 * 1000);
    
            try
            {
                FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
                StreamReader streamReader = new StreamReader(response.GetResponseStream());
    
                List<string> directories = new List<string>();
                string line = streamReader.ReadLine();
                while (!string.IsNullOrEmpty(line))
                {
                    directories.Add(line);
                    line = streamReader.ReadLine();
                }
            }
            catch (WebException we)
            {
                Console.WriteLine("Message:\r\n\t{0}\r\n", we.Message);
                Console.WriteLine("Response:\r\n\t{0}\r\n", we.Response);
                Console.WriteLine("Response code:\r\n\t{0}\r\n", ((FtpWebResponse)we.Response).StatusCode);
                Console.WriteLine("Response StatusDescription:\r\n\t{0}\r\n", ((FtpWebResponse)we.Response).StatusDescription);
                Console.WriteLine("Stacktrace:\r\n\t{0}\r\n", we.StackTrace);
            }
            catch (Exception e) { Console.WriteLine("Exception.ToString:\r\n\t{0}", e.ToString()); }
            finally { Console.ReadLine(); }
        }
    }
    

Each time I run this code, I receive the following output.

Message:

The operation has timed out.

Response:

System.Net.FtpWebResponse

Response Code:

Undefined

Response StatusDescription:

Stacktrace:

at Syste.NetFtpWebRequest.GetResponse() at ConsoleApplication1.Program.Main(String[] args) in c:\Visual Studio 2013\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line27

Dan R
  • 33
  • 8
  • 1
    If you haven't already, you can try setting the `Timeout` value to -1 which represents Infinity. Seems that has solved a similar issue for others as seen [here](https://stackoverflow.com/a/7665077/5803406). – devNull Feb 18 '19 at 20:30
  • @devNull I have tried setting `ftpRequest.Timeout = -1`. I then tried it with `.DownloadFile` and `.ListDirectory`; each try, letting it run for > 7 minutes before killing it. – Dan R Feb 18 '19 at 21:09

0 Answers0