Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

FtpWebResponse.Close Exception after large file donwloaded

$
0
0

Hello everyone,

Thanks for looking into it.

My problem is the following:

I donwload backup files from IIS FTP server almost all of them are 500-800 MBytes and one of them 4GBytes in length.

500-800 files downloaded almost all time well without any problem.

Main problem is that after 4GB file has been just donwloaded I try to close FtpWebResponse and get the following exception:

"downloaded: total bytes downloaded: 4321507840, detected file length: 4321507840, total files detected: 9 <- my string line

The underlying connection was closed: An unexpected error occurred on a
receive.

   at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   at System.Net.FtpWebRequest.RequestCallback(Object obj)
   at System.Net.CommandStream.InvokeRequestCallback(Object obj)
   at System.Net.CommandStream.Abort(Exception e)
   at System.Net.CommandStream.CheckContinuePipeline()
   at System.Net.FtpWebRequest.DataStreamClosed(CloseExState
closeState)
   at System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(CloseExState
closeState)
   at System.Net.FtpDataStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at System.Net.FtpWebResponse.Close()

Here is my code:

ftpRequest = FtpWebRequest.Create("ftp://aaa.bbb.cc.ddd:2121/" + CurrentFileName) as FtpWebRequest;
                            ftpRequest.Credentials = new NetworkCredential("un", "pwd");
                            ftpRequest.UsePassive = true;
                            ftpRequest.UseBinary = true;
                            ftpRequest.KeepAlive = true; 
                            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                            ftpRequest.ReadWriteTimeout *= 2;
                            ftpRequest.Timeout *= 2;

                            ftpResp = ftpRequest.GetResponse() as FtpWebResponse;

                            fs = new FileStream(Path.Combine(PathToSave, CurrentFileName + ".part"), FileMode.Create);
                            fs.Seek(0, SeekOrigin.Begin);
                            Stream ftpfs = ftpResp.GetResponseStream();

                            int bytesRed = 0;
                            int lastFullBufLength = 0;
                            
                            while (ftpfs.CanRead && (bytesRed = ftpfs.Read(buff, 0, buff.Length)) > 0)
                            {
                                fs.Write(buff, 0, bytesRed);
                                CurrentDownloaded += bytesRed;
                                if (CurrentDownloaded == CurrentFileLength)
                                    break;


                                if (bytesRed == buff.Length)
                                {
                                    lastFullBufLength = buff.Length;
                                    buff = new byte[buff.Length * 2];
                                }
                            }

                            fs.Close();
                            fs = null;

                            logger.WriteLine("downloaded: total bytes downloaded: {0}, detected file length: {1}, total files detected: {2}",
                                CurrentDownloaded, CurrentFileLength, TotalFiles);


                            if (File.Exists(Path.Combine(PathToSave, CurrentFileName)))
                                File.Delete(Path.Combine(PathToSave, CurrentFileName));
                            File.Move(Path.Combine(PathToSave, CurrentFileName + ".part"), Path.Combine(PathToSave, CurrentFileName));

                            TotalDownloadedFiles++;
                        }
                        catch (Exception ex)
                        {
                            isError = true;
                            logger.WriteLine(ex.Message);
                            logger.WriteLine(ex.StackTrace + "\r\n\r\n");
                        }
                        finally
                        {
                            logger.Flush();
                            if (fs != null)
                                fs.Close();

                            if (ftpResp != null)
                                try
                                {
                                    ftpResp.Close(); // <-- Here is the exception occures after 4GB file was downloaded
                                }
                                catch (Exception ex)
                                {
                                    logger.WriteLine(ex.Message);
                                    logger.WriteLine(ex.StackTrace + "\r\n\r\n");
                                }
                        }

 Any ideas or explanations, thank you

Assembler (.../z80/x86/x64...) the best language I've ever known



Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>