Try:
request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
题
I am downloading a file from an ftp server using FtpWebResponse. My code looks like this:
FtpWebRequest request;
try
{
request = (FtpWebRequest)FtpWebRequest.Create(path);
request.UseBinary = true;
request.Credentials = new NetworkCredential(user, passwd);
request.Method = WebRequestMethods.Ftp.DownloadFile;
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
MemoryStream ms = new MemoryStream();
using (Stream rs = (Stream)response.GetResponseStream())
{
rs.CopyTo(ms);
}
}
}
catch (Exception ex)
{
... error handling code here ...
}
Which worked fine - I was able to read the memory stream and see the file. I was testing, so I copied another file to the ftp server, deleted the old one, and renamed the new one to the same name, then re-ran my program. I was still seeing the old file - the one I had deleted from the ftp server - not the new one. So I deleted the new file - now there are no files on the ftp server. I re-run my program, and it runs without error and continues to show me the original file from the ftp server. If I ftp directly to the ftp server I verify that there are no files there...
What's happening? Is the connection cached? These symptoms have been evident for an hour or so now...
解决方案
Try:
request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);