'FtpWebRequest.GetResponse() takes forever

I made a simple program download files from my FTP server and usually this works well(I often reboot my laptop if not working and the program runs perfectly) with 0~1 failed trials. However, sometimes the program hangs and nothing happens, and I don't want to reboot my whole PC for that reason. What did I missed and what should I try?

Here is my code:

        while (true)
        {
            try
            {
                ftprequest = (FtpWebRequest)WebRequest.Create(url);
                ftprequest.Credentials = new NetworkCredential(id, pw);
                ftprequest.KeepAlive = false;
                ftprequest.Method = WebRequestMethods.Ftp.DownloadFile;
                ftprequest.UsePassive = false;
                ftprequest.Timeout = 2000;
                using (var resp = ftprequest.GetResponse())
                {
                    resp.Close();
                    break;
                }
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.Timeout && i < 5)
                {
                    i++;
                    Console.WriteLine("Try {0}: failed", i);
                    e.Response.Close();
                }
                else
                {
                    Console.WriteLine("Failed to connect to the server.");
                    Console.ReadKey(true); 
                    return;
                }
            }
        }


Solution 1:[1]

Call

ftprequest.Close()

explicitly before

using (var resp = ftprequest.GetResponse())

see https://docs.microsoft.com/en-us/dotnet/api/system.net.ftpwebrequest.getresponse?redirectedfrom=MSDN&view=netframework-4.8#examples

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Not Important