FTP Request timeout with NET Provider only

Asked

Viewed 88 times

-1

I use FTP, to download files to my system, it is a very simple code, using FTP REQUEST, however I am facing problems with who uses the NET provider(and some others however the Net is more evident)

try
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.Credentials = new NetworkCredential(usuario,senha);
            request.UseBinary = true;
            request.UsePassive = true;


            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
            {
                using (Stream rs = response.GetResponseStream())
                {
                    using (FileStream ws = new FileStream(local, FileMode.Create))
                    {
                        byte[] buffer = new byte[2048];
                        int bytesRead = rs.Read(buffer, 0, buffer.Length);

                        while (bytesRead > 0)
                        {
                            ws.Write(buffer, 0, bytesRead);
                            bytesRead = rs.Read(buffer, 0, buffer.Length);
                        }
                    }
                }
            }
        }
        catch
        {
            throw;
        }

In the request.Getresponse(), Exception returns, Time Limit of Operation Reached, I ran Telnet to test port 21, and it worked. So I believe there’s something I can do to fix this problem.

  • It could be the firewall that blocked your C# program to access other domains and ports. There’s no way to know. Ran telnet and C# on the same machine?

  • @Guilhermenascimento with the same machine using a different internet than certain, so it is not the Windows Firewall, perhaps the firewall in conjunction with the Network?

  • No, I believe it’s the machine’s firewall, when it accesses a WIFI network it can enter Public Network mode and other Wifi in particular network, which changes the default rules of the firewall, but has no way of stating. Anyway the problem doesn’t seem to be in the code.

  • I ran Telnet, and C# on the same machine and with the same internet.

  • In win 10 each wifi network (SSID) can be linked to a type of network, private, company or publish, so if it worked on another network is pq should be as private and if it does not work on the current network is pq should be as published, but this is not a matter of the network but of the firewall rules of each network, of which you can "customize", and probably the first time you ran your C# vc must have applied some permission of a windows window that appeared and did not notice

  • @Guilhermenascimento I will try to verify this aspect.

  • If you have someone who has knowledge of networks (even Windows) there helps, it is good you learn, but if you do not know or risk a lot to move, ask someone to do it, although it is not very complicated is a dangerous place to move, can cause a huge problem

  • Read the documentation in the part about DNS: https://docs.microsoft.com/pt-br/dotnet/api/system.net.ftpwebrequest.timeout?view=netframework-4.8#coment-rios

  • I will read @Brunowarmling, I will also try to change the Timeout to see if there is any difference in the error return.

  • 1

    According to what it says there, Timeout is infinite, so could be problem when trying to solve the DNS... Try using direct IP to see if the same problem occurs.

  • @Brunowarmling I will test switch to ip, too.

  • 1

    @Brunowarmling killed the riddle, the problem is in trying to solve the DNS, I will do some more tests and put the solution.

Show 7 more comments

1 answer

0

The problem, it was in the resolution of dns the ISP, it was not able to solve the DNS of the Server address, doing the test directly with the IP, the same goes to work. However as it is a hosting with IP Dynamico, it is necessary to recover the IP of the moment of Access.

  IPHostEntry hostname = Dns.GetHostEntry("servidor.com.br");
  IPAddress[] ip = hostname.AddressList;

Doing so is returned the IP, and use it instead of the NAME, making the function work normally in both cases.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.