What is the correct way to clear resources after an FTP operation?

Once I upload a file using FTP, what should I do? Usually I just close two threads, that is:

sourceStream.Close(); requestStream.Close(); 

What will be the effect if they remain open? Will other users log in using the same credentials? Will I be able to log in again?

Here is the complete code for a better understanding:

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create( "bbbbb" + "bbbbbb" + "bbb/" + hj + "/" + hjj + ".txt"); request.Credentials = new NetworkCredential("bbbbb", "bbbbbb"); request.Method = WebRequestMethods.Ftp.UploadFile; request.UsePassive = true; StreamReader sourceStream = new StreamReader(j + @"oo.txt"); byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); sourceStream.Close(); request.ContentLength = fileContents.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); sourceStream.Close(); requestStream.Close(); 

In addition, I sometimes only have the request.open method, which does not have a close() method assigned, and then I use request.Abort() at the very end of the FTP operation. Does this make sense?

+4
source share
2 answers

Well, the MSDN documentation reads:

When using the FtpWebRequest object to upload a file to the server, you must write the contents of the file to the request stream obtained by calling the GetRequestStream method or its asynchronous copies, the BeginGetRequestStream and EndGetRequestStream methods. You must write to the stream and close the stream before sending the request.

which indicates that bad things can happen if you do not close the request flow. If you do not close the source stream, you will probably end up blocking the file that denies access for future requests and / or other users, although this depends on the underlying OS and the file sharing mode.

Regarding the close of the request: I have not used WebRequest and Co, however, following the above MSDN documentation, it seems that the template you should follow is

  • Create a request
  • Get request stream
  • Write your details
  • Close request stream
  • Get an answer
  • Close the response object (which I assume terminates the request)

Perhaps this is a good idea if you do not know otherwise.

+1
source

In this case, a little cleaner, uses fewer threads and properly closes them when this is done.

 Uri requestUri = new Uri(string.Concat("bbbbb", "bbbbbb", "bbb/", hj, "/", hjj, ".txt")); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(requestUri); request.Credentials = new NetworkCredential("bbbbb", "bbbbbb"); request.Method = WebRequestMethods.Ftp.UploadFile; request.UsePassive = true; byte[] fileContents = File.ReadAllBytes(@"oo.txt"); request.ContentLength = fileContents.Length; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(fileContents, 0, fileContents.Length); } 

WebRequest.Abort designed to terminate asynchronous operations that you do not have. Do not call it here. Please examine the using statement that I included, as suggested in the comments of Mitch Wheat. It will automatically delete the stream object.

Like other questions, of course, someone can use the same credentials to log into the FTP server, but not as a result of your code. The same credentials will always succeed. I think you are worried that the connection is active, but this is a problem for the server.

0
source

Source: https://habr.com/ru/post/1347405/


All Articles