Results 1 to 4 of 4

Thread: FTP connection problem - ftp server now requires SFTP connection

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    10

    Exclamation FTP connection problem - ftp server now requires SFTP connection

    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.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: FTP connection problem - ftp server now requires SFTP connection

    found this thread :
    http://www.codeproject.com/Messages/...est-class.aspx

    This post wasa couple of replies deep...
    Go to ParentHere is the C# version - I'm really not interested in getting into a C# is better then vb debate. I just want get this working so I can move on to the next item on my to do list.
    private void List(string listUrl, string username, string password)
    {
    StreamReader reader = null;
    FtpWebRequest listRequest = WebRequest.Create(new System.Uri(listUrl));
    System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
    listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    listRequest.KeepAlive = true;
    listRequest.EnableSsl = true;
    listRequest.Credentials = new NetworkCredential(username, password);
    FtpWebResponse listResponse = listRequest.GetResponse();
    reader = new StreamReader(listResponse.GetResponseStream());
    }

    public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
    {
    public bool CheckValidationResult(System.Net.ServicePoint srvPoint, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Net.WebRequest request, int certificateProblem)
    {
    return true;
    }

    }
    It's in C#, but I think the key points are what I've marked...

    shouldn't be too hard to move it to VB.NET

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: FTP connection problem - ftp server now requires SFTP connection

    Just to tack on, yes, 1and1 requires a SFTP connection now.

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    10

    Re: FTP connection problem - ftp server now requires SFTP connection

    Thank you both for your replies.

    Luckily I decided to test my program & FileZilla at work yesterday to see if I still had
    the same problem. Low and behold it transferred my files OK. So that lead me to believe
    it could have been something at my end that was causing the problem.

    When I got home I did a hard reboot of my router and then tried my program and FileZilla
    and voila! it started transferring the files again. Therefore it must have been a problem with
    my router and after the reboot it cured it.

    So thankfully I do not need to re-code my program =)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width