Problems with FTP
Hello Everyone and thanks for your help in advance. I am working on an FTP client program to download some fairly large images files from an FTP site. The download begins correctly but nearly always, I receive the following exception:
System.IO.IOException: Unable to read data from the transport connection: 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.
On a few rare occasions, the download will complete successfully. The code I am using is the following:
Dim objFTPRequest As FtpWebRequest = CType(WebRequest.Create(URL), FtpWebRequest)
objFTPRequest.Credentials = New NetworkCredential(UserName, Password)
objFTPRequest.Method = WebRequestMethods.Ftp.DownloadFile
Dim objFTPResponse As FtpWebResponse = CType(objFTPRequest.GetResponse, FtpWebResponse)
Dim objStream As Stream = objFTPResponse.GetResponseStream
'loop to read & write to file
Dim fs As New IO.FileStream(LocalFile, IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = objStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0 'see Note(1)
objStream.Close()
fs.Flush()
fs.Close()
objStream.Close()
objFTPResponse.Close()
I am not sure what I need to do to get this working. Any help on this topic would be greatly appreciated. Thanks.
|