locked
How to reuse FtpWebRequest and close it until all operations finished RRS feed

  • Question

  • User-86041214 posted

    Hi:

    I have an problem about how to reuse FtpWebRequest and close it until all operations finished.

    The following is the example of the FtpWebRequest in code project:

    https://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class 

    The main problem is when the FtpWebRequest opening and connecting to FTP server, and when to 

    Close it when all operations are finished. Like the following scenarios:

    Construct a FtpWebRequest class, like ftp object, then:

    1.ftp.connect, 2.ftp.checkDirectoryExist, 3.ftp.checkFileExist,...

    and finally, ftp.close;

    Is there any instruction or suggestion for that, thanks a lot.

    Friday, November 10, 2017 7:57 AM

Answers

  • User-335504541 posted

    Hi abramhum.c.l,

    And could I do multiple methods before close()?

    I'm afraid you can't. You need to use 

     (FtpWebRequest)WebRequest.Create("ftp://xxx.xxx.xxx.xxx:21/xxx.xx")

    to create multiple requests for multiple methods.

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 15, 2017 1:15 AM

All replies

  • User-335504541 posted

    Hi abramhum.c.l,

    Do you want to know how to open and close the FtpWebRequest?

    If so, please try to refer to the following code:

                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
                request.Method = WebRequestMethods.Ftp.DownloadFile;
    
                // This example assumes the FTP site uses anonymous logon.  
                request.Credentials = new NetworkCredential("anonymous", "janeDoe@contoso.com");
    
                // Check file exist 
                try
                {
                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                    Stream responseStream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(responseStream);
                    Console.WriteLine(reader.ReadToEnd());
                    Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
                    reader.Close();
                    response.Close();
                }
                catch (WebException ex)
                {
                    FtpWebResponse response = (FtpWebResponse)ex.Response;
                    if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                    {
                        //File not exist
                    }                   
                }

    And here is the reference:

    https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-download-files-with-ftp

    https://stackoverflow.com/questions/347897/how-to-check-if-file-exists-on-ftp-before-ftpwebrequest

    Best Regards,

    Billy

    Monday, November 13, 2017 6:02 AM
  • User-86041214 posted

    NO, I hope to know when the FtpWebRequest open connection, and if the connection exist, 

    could I add multiple operations, like checkfileExist, AddFile,DownloadFile, ..and so on.

    And then finally, close the connection, set the FtpWebRequest object to null.

    But he problem is all the examples I found didn't talk about that, and some articles 

    say it can't be this way. So, I hope to find the correct answer, and make sure if 

    FtpWebRequest can do it. Thanks.

    Tuesday, November 14, 2017 12:23 AM
  • User-335504541 posted

    Hi abramhum.c.l,

    NO, I hope to know when the FtpWebRequest open connection, and if the connection exist, 

    could I add multiple operations, like checkfileExist, AddFile,DownloadFile, ..and so on.

    The connection open when the

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

    executed.

    If you want to add file or download file, you need to set the request.Method before open the connection.

    For example:

     FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://xxx.xxx.xxx.xxx:21/test.txt");
     request.Method = WebRequestMethods.Ftp.DownloadFile;

    https://docs.microsoft.com/en-us/dotnet/api/system.net.ftpwebrequest.method?view=netframework-4.7.1

    And if you want to close the connection, please try to use:

    response.Close();

    Best Regards,

    Billy

    Tuesday, November 14, 2017 12:28 PM
  • User-86041214 posted

    Thanks. And could I do multiple methods before close()?

    Tuesday, November 14, 2017 11:34 PM
  • User-335504541 posted

    Hi abramhum.c.l,

    And could I do multiple methods before close()?

    I'm afraid you can't. You need to use 

     (FtpWebRequest)WebRequest.Create("ftp://xxx.xxx.xxx.xxx:21/xxx.xx")

    to create multiple requests for multiple methods.

    Best Regards,

    Billy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 15, 2017 1:15 AM
  • User-86041214 posted

    Thanks for your help.

    Wednesday, November 15, 2017 6:28 AM
  • User-86041214 posted

    is there any other way micsoft given can do that? Thanks.

    Friday, November 17, 2017 2:28 AM