12
ftpサーバからファイルをダウンロードしたいとします。 私がftpftpサーバからファイルをダウンロードするときに「下にある接続が閉じられました」
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);
}
}
からファイルをダウンロードするには、次のコードを書かれている。しかし、私に例外を与えて、このコードの実行時に:基になる接続が閉じられました
を:予期しないエラーが受信 に発生しました。問題は私を助けることができるもの
...
キープアライブプロパティをfalseに設定すると、あなたがCloseメソッドを呼び出したときに、制御接続がクローズされます。 この例外はどの回線で発生しますか? Timeoutプロパティ(Infinity)のデフォルト値を使用しようとしましたか? – Ofiris
ファイアウォールの問題かもしれません。パッシブモードの代わりにアクティブモードを使用しようとしましたか? – Arjen
これはサーバーまたはローカルのtcp/ipの問題(ファイアウォール、ウイルス対策など)に問題があります。コードは正常に動作します。例えば、これは私のディスクにccrisconv.txtファイルを作成します: 'downloadFile(" ftp://ftp.nlm.nih.gov/ "、" nlmdata/sample/ccris/ccrisconv.txt "、null 、null、 "ccrisconv.txt"); ' –