C# - FtpWebRequest - Multiple requests over the same connection/login

后端 未结 2 872
故里飘歌
故里飘歌 2021-01-12 12:04

I want to loop on a FTP folder for check if a file has arrived

I do:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(\"ftp://localhost:8080         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-12 12:42

    Sorry, I missed it, you're only issuing one request and trying to get a response multiple times. Try the code below:

    while(true)
    {
        FtpWebRequest request =     (FtpWebRequest)WebRequest.Create("ftp://localhost:8080");
        request.Credentials = new NetworkCredential("anonymous", "");
        request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    
        using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
        using (Stream responseStream = response.GetResponseStream())
        using (StreamReader reader = new StreamReader(responseStream))
        {
            Console.WriteLine(reader.ReadToEnd());
    
            reader.Close();
            response.Close();
        }
    }
    

    You should add a pause of some sort at the end of each loop though. You don't want to bombard the server.

提交回复
热议问题