2012-04-18 28 views
2

FTPサーバーに接続してファイルをダウンロードしようとしていますが、何か問題があります。FtpWebRequestは、長い転送の終わりにWebExceptionをスローするのはなぜですか?

もともと私がしたFtpWebRequestに切り替えるとするのFileStreamを使用してみました私はWebClient.DownloadFileAsyncを使用し、それは小さなファイルのためにうまく働いたが、大きなファイルは、ダウンロードの最後に到達するだろうが、DownloadFileCompletedを呼び出すことはありません...

標準的な方法を使用してファイルを書き込む:

var FtpRequest = WebRequest.Create(uri) as FtpWebRequest 
FtpRequest.Credentials = _Credentials; 
FtpRequest.Method = WebRequestMethods.Ftp.DownloadFile; 

using(var InputResponse = (FtpWebResponse)FtpRequest.GetResponse()) 
using (var InputStream = InputResponse.GetResponseStream()) 
using (var OutputStream = new FileStream(_DownloadDirectory + "\\" + fileName, FileMode.Create)) 
{ 
    var Buffer = new byte[1024]; 
    int TotalReadBytesCount = 0; 
    int ReadBytesCount; 
    while ((ReadBytesCount = InputStream.Read(Buffer, 0, Buffer.Length)) > 0) 
    { 
     OutputStream.Write(Buffer, 0, ReadBytesCount); 
     TotalReadBytesCount += ReadBytesCount; 
     var Progress = (int)(((double)TotalReadBytesCount/(double)FileSize) * 100); 
     UpdateProgressBar(progressBar, Progress); 
    } 
} 

この1つはまた、小さなファイルで正常に動作しますが、大きなファイルとそれがファイル全体をダウンロードし、その後、私はInputStream.ReadでSystem.Net.WebExceptionを取得します。

EDIT:コードがどのように構造化されているかによって、例外が移動します。「使用」ステートメントを削除し、各ストリームを閉じると、最後のx.close()によって例外がスローされます。また、TotalBytesReceived ==はfileSizeであり、ダウンロードは技術的に完了しています。 END EDIT

内部例外:内部接続が閉じられました:受信時に予期しないエラーが発生しました。

System.Net Verbose: 0 : [2440] WebRequest::Create(ftp://ftp.******.com/) 
System.Net Information: 0 : [2440] FtpWebRequest#63621045::.ctor(ftp://ftp.******.com/) 
System.Net Verbose: 0 : [2440] Exiting WebRequest::Create()  -> FtpWebRequest#63621045 
System.Net Verbose: 0 : [2440] FtpWebRequest#63621045::GetResponse() 
System.Net Information: 0 : [2440] FtpWebRequest#63621045::GetResponse(Method=SIZE.) 
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Created connection from ***.**.***.**:***** to **.**.**.**:**. 
System.Net Information: 0 : [2440] Associating FtpWebRequest#63621045 with FtpControlStream#44374744 
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Received response [220 Microsoft FTP Service] 
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Sending command [USER ******] 
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Received response [331 Password required for cashipftp.] 
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Sending command [PASS ********] 
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Received response [230 User logged in.] 
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Sending command [OPTS utf8 on] 
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Received response [200 OPTS UTF8 command successful - UTF8 encoding now ON.] 
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Sending command [PWD] 
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Received response [257 "/" is current directory.] 
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Sending command [SIZE Superintendents/MSK/Stock Keeper_be.zip] 
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Received response [213 96601015] 
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Sending command [QUIT] 
System.Net Information: 0 : [2440] FtpControlStream#44374744 - Received response [221 Goodbye.] 
System.Net Information: 0 : [2440] FtpWebRequest#63621045::(Releasing FTP connection#44374744.) 
System.Net Verbose: 0 : [2440] Exiting FtpWebRequest#63621045::GetResponse() 
System.Net Verbose: 0 : [2440] FtpWebResponse#50706457::Close() 
System.Net Verbose: 0 : [2440] Exiting FtpWebResponse#50706457::Close() 
System.Net Verbose: 0 : [2440] WebRequest::Create(ftp://ftp.******.com/) 
System.Net Information: 0 : [2440] FtpWebRequest#89223::.ctor(ftp://ftp.*****.com/) 
System.Net Verbose: 0 : [2440] Exiting WebRequest::Create()  -> FtpWebRequest#89223 
System.Net Verbose: 0 : [2440] FtpWebRequest#89223::GetResponse() 
System.Net Information: 0 : [2440] FtpWebRequest#89223::GetResponse(Method=RETR.) 
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Created connection from ***.**.***.**:***** to **.**.**.**:**. 
System.Net Information: 0 : [2440] Associating FtpWebRequest#89223 with FtpControlStream#4015056 
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Received response [220 Microsoft FTP Service] 
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Sending command [USER ******] 
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Received response [331 Password required for cashipftp.] 
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Sending command [PASS ********] 
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Received response [230 User logged in.] 
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Sending command [OPTS utf8 on] 
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Received response [200 OPTS UTF8 command successful - UTF8 encoding now ON.] 
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Sending command [PWD] 
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Received response [257 "/" is current directory.] 
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Sending command [TYPE I] 
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Received response [200 Type set to I.] 
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Sending command [PASV] 
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Received response [227 Entering Passive Mode (77,44,60,82,106,69).] 
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Sending command [RETR *****] 
System.Net Information: 0 : [2440] FtpControlStream#4015056 - Received response [125 Data connection already open; Transfer starting.] 
System.Net Verbose: 0 : [2440] Exiting FtpWebRequest#89223::GetResponse() 
System.Net Information: 0 : [1312] ServicePoint#42865679 - Closed as idle. 
System.Net.Sockets Error: 0 : [2440] Socket#10316078::UpdateStatusAfterSocketError() - TimedOut 
System.Net.Sockets Error: 0 : [2440] Exception in Socket#10316078::Receive - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. 
System.Net Information: 0 : [2440] FtpWebRequest#89223::(Releasing FTP connection#4015056.) 
System.Net Verbose: 0 : [2440] FtpWebResponse#19201658::Close() 
System.Net Verbose: 0 : [2440] Exiting FtpWebResponse#19201658::Close() 

EDIT 19/04/2012:

system.Net.Tracingを有効にした後、私は、次のログファイルを持って、私はftpのフィルターで行くをWiresharkを与えたと私は、私もいくつかのより多くの情報を発見したかもしれませんどこに行くのか分かりません...

EDIT:新しい情報...もう少し光を当てる別のフォーラムでいくつかの情報を見つけましたが、答えはまだありません。ファイルサイズは違いはありません。小さなファイルをダウンロードしてもループ内のスレッドをスリープさせてダウンロード時間を延長するのと同じ効果があります。 x時間(他のポストレコンス100s)よりも長い操作はすべて終了し、すぐにタイムアウトするようです。また、私はデバッガをステップ実行するときに、受信バイトの総数がファイルサイズと等しいことに注意しました。これは、ファイルの最後を読み取るようにして、サーバーが決して見つからないそれを伝えます! END EDIT

ftpサーバは、226 - データ接続を閉じると応答します。要求されたファイルの処理は成功しました(たとえば、ファイル転送またはファイルの中止)。

しかし私がwiresharkを使用したとき、私はそれがその前に[TCP Retransmission]と入っていないことがわかります。まだ何が...まだグーグルであるかわからない。しかし、私はそれが関連性があると確信しています。

タイムアウトや早い時期に閉鎖されているようですが、FTPと多少の両方に新しいことがあります。どこから進めるべきかわかりません。

私が試してみました、多くの異なるもの:

  • キープアライブ偽=
  • SetPointManager.MaxIdleTime
  • 増やすサーバーのタイムアウトそれはそれのように思える

は私をキックオフサーバーですしかしそれはfilezillaとうまく動作するので、これを回避するには何らかの方法が必要です。

EDIT 20/04/2012:filezilla(またはエクスプローラ)でうまく動作しません。エクスプローラを使用してftpからダウンロードしようとしましたが、操作がタイムアウトしたというメッセージが表示されました。 Filezillaはファイルをダウンロードしてからタイムアウトしますが、RESTコマンドで再び巧みに試します。私はこれを実装することが理想的なソウルシングになる可能性があると思うので、それを理解するためには外しています。成功すれば答えとして投稿します。 END EDIT

WebExceptionが渡されるまで、ダウンロードされたファイルへのアクセスはブロックされます。 WebClientの場合、タイムアウトには影響しません。これは、長時間100%でハングするだけです。例外をスローします。 FtpRequestタイムアウトを1000に設定して例外を処理すると、ファイルが完全にダウンロードされたように見え、破損することなくファイルを抽出できます。

誰かがヒントや指針を持っている場合、あるいはそれ以上の解決策があれば:)私はとても感謝しています。すべての入力が受け入れられます。

はあなたに私はできるだけ多くの情報を提供しようとしたが、あなたがより多くの私に教えて必要がある場合

P.Sありがとうございます。

+0

関連:[FTPWebRequest is broken](http://mattmitchell.com.au/ftpwebrequest-is-broken/)。私もこの問題に取り組んでいます。私が 'netstat'を実行すると、すぐに応答を処理しても、何千もの接続が見えて、' keepalive'は 'false'です。 .NETリファレンスソースで 'FTPWebRequest'を調べると、意図的に接続をオープンに保つように設計されているようですが、これらのポートが再利用されることを期待していますが、実際には毎回異なるポートを使用しています... – jrh

+0

..私の現在の理論は、サーバが接続を拒絶し始めるまで毎回異なるポートを試し続けているということです。私は問題をテストするために膨大な量のファイルを送信するプログラムを書いた。文字通りポート番号が不足している可能性があります.netstatは49157から65535のポートを示しています。一度に1つのファイルしか送信しません。 – jrh

答えて

0

ソースアプリケーションの設定でダウンロードのサイズが制限されているかどうかを確認してください。

+0

app.configを意味する場合は?そこにあるのは、トレースと2つの設定です。私はその効果をhttpRuntime設定について読むが、これはFTPです。私が変えることができる同様の設定がありますか? –

関連する問題