I'm getting a timeout error at this line of code in the sample:
Dim _Stream As System.IO.Stream = _WebRequest.GetRequestStream()
My PC was recently switched over to Windows 10. The previous operating system was Windows 7.
The app was developed under Windows 7. The entire class is below.
I need to know the possible sources of the timeout. The app worked just fine under Windows 7. When my PC was switched to Windows 10, the app failed.
Public Class ftpUpload
''' <summary>
''' Methods to upload file to FTP Server
''' </summary>
''' <param name="_FileName">local source file name</param>
''' <param name="_UploadPath">Upload FTP path including Host name. DON'T FORGET TO INCLUDE THE FILE NAME OF THE TARGET FILE!!
''' The one for Carfax is "ftp://ftp.carfax.com/" plus the file name (yyyymmdd-yyyymmdd.xml) </param>
''' <param name="_FTPUser">FTP login username</param>
''' <param name="_FTPPass">FTP login password</param>
Public Sub UploadFile(ByVal _FileName As String,
ByVal _UploadPath As String,
ByVal _FTPUser As String,
ByVal _FTPPass As String)
' get the file information
Dim _FileInfo As New System.IO.FileInfo(_FileName)
' Create FtpWebRequest object from the Uri provided
'Dim _FtpWebRequest As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)
Dim _WebRequest As System.Net.FtpWebRequest = CType(System.Net.WebRequest.Create(New Uri(_UploadPath)), System.Net.FtpWebRequest)
' Provide the WebPermission Credintials
_WebRequest.Credentials = New System.Net.NetworkCredential(_FTPUser, _FTPPass)
' By default KeepAlive is true, where the control connection is not closed
' after a command is executed.
_WebRequest.KeepAlive = False
_WebRequest.UsePassive = False
' set timeout for 30 seconds (30 * 1000)
_WebRequest.Timeout = 30000
' Specify the command to be executed.
_WebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
' Specify the data transfer type.
_WebRequest.UseBinary = True
' Notify the server about the size of the uploaded file
_WebRequest.ContentLength = _FileInfo.Length
' The buffer size is set to 2kb
Dim buffLength As Integer = 2048
Dim buff(buffLength - 1) As Byte
' Opens a file stream (System.IO.FileStream) to read the file to be uploaded
Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()
Try
' Stream to which the file to be upload is written
Dim _Stream As System.IO.Stream = _WebRequest.GetRequestStream()
' Read from the file stream 2kb at a time
Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)
' Till Stream content ends
Do While contentLen <> 0
' Write Content from the file stream to the FTP Upload Stream
_Stream.Write(buff, 0, contentLen)
contentLen = _FileStream.Read(buff, 0, buffLength)
Loop
' Close the file stream and the Request Stream
_Stream.Close()
_Stream.Dispose()
_FileStream.Close()
_FileStream.Dispose()
GC.Collect()
Catch ex As System.Net.WebException
MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class
.UsePassive = True
. The Stream Read/Write proc is questionable._WebRequest.EnableSsl = True
? Are you sure it's not required? The defaul is False.KeepAlive = True
is the default value because .Net uses connection pool and it manages these connections thorough a ServicePoint class. In a FTP connection,KeepAlive = False
also means close the connection..UsePassive = False
means that the FTP Server will not be the only active party: your app need to send commands accordingly, and you're not sending any, which is the standard.UsePassive = True
behaviour. You don't need to listen to opinions, you need to read the Docs about it.