2012-04-18 1 views
2

FTP 서버에 연결하고 파일을 다운로드하려고하는데 문제가 있습니다. 왜 FtpWebRequest는 긴 전송이 끝날 때 정확히 WebException을 던집니까?

원래 나는 WebClient.DownloadFileAsync를 사용하고 작은 파일에 대해 잘 작동하지만, 큰 파일은 내가에서는 FtpWebRequest로 전환하고하여 FileStream을 사용하여 시도 ... 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); 
    } 
} 

이 하나는 작은 파일이 잘 작동하지만, 큰 파일로 전체 파일을 다운로드하고 다음 나는 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() 

편집 19/04/2012 :

system.Net.Tracing을 활성화 한 후 나는 다음과 같은 로그 파일을 가지고 나는 FTP 필터와 이동을 Wireshark를 포기하고 I는 I 또한 좀 더 많은 정보를 발견했을 수 있습니다 어디로 가야할지 모르겠다 ...

EDIT : 새로운 정보 ... 조금 더 빛을 비추는 다른 포럼에 대한 정보를 찾았지만 여전히 답변을 제공하지 않습니다 ... 그것은 파일 크기는 차이가 없습니다. 작은 파일을 다운로드하지만 다운로드 시간을 연장하기 위해 루프에서 스레드를 잠자면 같은 효과가 있습니다. x 시간 (다른 게시물 수는 100 초임)보다 긴 작업은 끝까지 도달하고 즉시 시간 초과됩니다. 또한 디버거를 통해 스테핑 할 때 전체 수신 된 바이트가 파일 크기와 같을 때 정확하게 파일의 마지막을 읽으려고 시도 할 때 서버가 절대로 발견되지 않기 때문에 아무 것도 남지 않았 음을 알았습니다 그것을 말해! END EDIT

ftp 서버는 226 - Closing data connection으로 응답한다고 가정합니다. 요청 된 파일 작업이 성공했습니다 (예 : 파일 전송 또는 파일 중단).

그러나 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 사이의 포트를 보여줍니다. 한 번에 하나의 파일 만 전송합니다. – jrh

답변

0

원본 응용 프로그램 구성이 다운로드 크기를 제한하는지 확인하십시오.

+0

app.config의 의미라면? ATM에있는 모든 것은 추적 및 두 가지 설정입니다. 그 효과에 대한 httpRuntime 설정에 대해 읽었지만 FTP입니다. 비슷한 설정을 변경할 수 있습니까? –

관련 문제