Hi,

I wonder if someone can help on this FTP problem:

I wrote a program in Visual Basic 2010 back in 2013 that uploads local files to my FTP site, which has worked
flawlessly for the past 3 years. The problem I have is that over the past week the uploads have started failing.

I thought that there may be a problem on the web hosting companies servers, so checked
the status and all seems OK. So I then thought maybe something in my code had messed up
(even though it had been running fine for years), so I decided to try and connect to my hosted
ftp server via FileZilla. When I connected and tried to upload files I was also getting the same
problem as that with my program.

The error FileZilla returned was:
"The data connection could not be established: ECONNREFUSED - Connection refused by server"

So I Googled this error and it seems that the ftp server (which is a Linux Server), may now require the connection to be
via SFTP rather than just the standard FTP connection.

I had a look at my ftp settings on the web hosting companies site, and it now states:

Your SFTP account data:
Server : #########.1and1-data.host
Protocol : SFTP
Port : 22

So from the looks of it they have changed to SFTP connections, which is now causing me problems connecting!.



The current method I use to upload files is similar to that shown below. The code shown is from a 'test button'
within my program that can be click to check to make sure all FTP details/connection works, prior to the main
program running.



Code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click


        Try

            'Sets test variables on this form 3 to user defined
            FTPtest_URL = TextBox2.Text
            FTPtest_UserName = TextBox3.Text
            FTPtest_Password = TextBox4.Text

            '// need to have file name, in this case "ftptestfile.txt" the same in the below ftp url as well as (see other remark below), also make sure folder exists on FTP site.
            Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://" + FTPtest_URL + "ftptestfile.txt"), System.Net.FtpWebRequest)
            request.Credentials = New System.Net.NetworkCredential(FTPtest_UserName, FTPtest_Password) '// put username and password in here.
            request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
            request.UsePassive = True '// Set to use FTP PASSIVE (PASV) transfer mode

            '// in this Dim.. i.e "ftptestfile.txt" (for application directory, or "c:\ftptestfile.txt" to specify a drive/folder
            Dim file() As Byte = System.IO.File.ReadAllBytes(My.Application.Info.DirectoryPath & "\ftptestfile.txt")

            Dim strz As System.IO.Stream = request.GetRequestStream()
            strz.Write(file, 0, file.Length)
            strz.Close()
            strz.Dispose()

            'If FTP connection opened and file uploaded then present the following message:
            MessageBox.Show("Test successful. The following file has been uploaded to your website:" & vbCrLf & "" & vbCrLf & "ftptestfile.txt", ".::[ FTP tested OK ]::.")

        Catch ex As Exception

            MessageBox.Show("An error has occurred while trying to upload a testfile to your FTP site." & vbCrLf & "Please check your FTP credentials and internet connection then try again." & vbCrLf & vbCrLf & "NOTE:" & vbCrLf & "The error message has been written to the following local file" & vbCrLf & " (within the application folder): ..\logfiles\FTP_Error.log", ".::[ FTP Error ]::.")



            'creates a FTP log file and appends Date and Time stamp.
            'which has to reside in the application folder

            Dim sw As IO.StreamWriter
            sw = IO.File.AppendText(My.Application.Info.DirectoryPath & "\logfiles\FTP_Error.log")
            sw.WriteLine("FTP (Test) Error:  " & DateTime.Now)
            sw.WriteLine("")
            sw.WriteLine(ex.ToString)
            sw.WriteLine("")
            sw.WriteLine("")
            sw.Flush()
            sw.Close()

        End Try

    End Sub

So onto my question, which I hope someone can answer:

Does anyone know how I can edit my code above to allow it to connect and upload files via SFTP
rather than FTP, which is currently causing errors.

Any help or pointers would be greatly appreciated.