我想从 ftp 服务器下载文件。我已经编写了以下代码来从 ftp 下载文件
public void downloadFile(string FTPAddress, string filename, string username, string password, string destFile)
{
try
{
FtpWebRequest request = FtpWebRequest.Create(FTPAddress + filename) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.UseBinary = true;
request.KeepAlive = false; //close the connection when done
request.Timeout = 60000;
//Streams
using (var response = request.GetResponse())
{
using (Stream reader = response.GetResponseStream())
{
byte[] buffer = new byte[1024];
using (Stream streamFile = File.Create(destFile))
{
while (true)
{
int bytesRead = reader.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
break;
}
else
{
streamFile.Write(buffer, 0, bytesRead);
}
}
}
}
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
但是当运行这段代码给我异常时:
底层连接已关闭:接收时发生意外错误。
有什么问题可以帮我...