Hi, I have problem with FTP connection.
There is a loop where I retry to download a file (resume) if there is an exception (for example the network is down).
When I try to resume download after disconnection of the network I get a timeout.
The timeout occour after response.GetRes ponseStream();
The problem is that i can't close responseStream and response.
How can i solve this problem?
Thanks
This is the code:
//Here I get the error on timeout
There is a loop where I retry to download a file (resume) if there is an exception (for example the network is down).
When I try to resume download after disconnection of the network I get a timeout.
The timeout occour after response.GetRes ponseStream();
The problem is that i can't close responseStream and response.
How can i solve this problem?
Thanks
This is the code:
Code:
while (true)
{
try
{
request = (FtpWebRequest)WebRequest.Create(new Uri(p_ftp_down + p_nomeFile));
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Timeout = 10000;
request.ReadWriteTimeout = 10000;
ServicePoint sp = request.ServicePoint;
sp.ConnectionLimit = 1000;
sp.MaxIdleTime = 10000;
request.UseBinary = true;
request.Credentials = new NetworkCredential(p_user_down, p_psd_down);
if (riprova == false)
{
newFile = new FileStream(pathfile + p_nomeFile, FileMode.Create);
}
else
{
_File = new FileInfo(pathfile + p_nomeFile);
request.ContentOffset = _File.Length;
progress.Value = Convert.ToInt32(_File.Length);
_File = null;
newFile = new FileStream(pathfile + p_nomeFile, FileMode.Append, FileAccess.Write);
}
response = (FtpWebResponse)request.GetResponse();
Code:
responseStream = response.GetResponseStream();
byte[] buffer = new byte[1024];
responseStream.Flush();
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
progress.PerformStep();
newFile.Write(buffer, 0, bytesRead);
while (bytesRead > 0)
{
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
newFile.Write(buffer, 0, bytesRead);
progress.PerformStep();
}
responseStream.Close();
response.Close();
newFile.Close();
infinito = false;
ris = true;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + ex.InnerException);
if (newFile != null)
newFile.Close();
riprova = true;
System.Threading.Thread.Sleep(5000);
ris = false;
}
}
return ris;
}