3.1.4 - Auto-load lines and output via FTP from STHS

This section contains all the bug reports that have been solved in version 2 or version 3. / Cette section contient tous les rapports d'erreur qui ont été résolu dans la version 2 ou version 3.
JimToupet
STHS Dynamic Website Contributor
Posts: 363
Joined: Wed Jul 02, 2014 10:55 am

3.1.4 - Auto-load lines and output via FTP from STHS

Post by JimToupet »

Since this morning I can't autoload lines from my FTP or output to FTP since the webhoster ask for TLS FTP communication only. Any plan to implement TLS in STHS ?

And of course, my GM can't neither put lines on the FTP via the client.

TLS become a standard and should be implement otherwise this feature will become obselete.
SimonT
STHS Owner / Propriétaire du STHS
Posts: 14756
Joined: Sat Oct 08, 2005 4:18 pm
Location: Montreal, Canada
Contact:

Re: Auto-load lines and output via FTP from STHS

Post by SimonT »

Hi.

I'll have to look at it in the couple of day. I have try the EnableSSl method on the System.Net.FtpWebRequest but it fail on my hosting provider when I try that.
-SimonT
Forum Administrator / Administrateur du Forum
STHS Owner / Propriètaire du STHS
English V2 & V3 Manual - Manuel V2 & V3 Français
JimToupet
STHS Dynamic Website Contributor
Posts: 363
Joined: Wed Jul 02, 2014 10:55 am

Re: Auto-load lines and output via FTP from STHS

Post by JimToupet »

Hoster (mine do) probably use TLS 1.2 (the latest) and as I can read, that protocol is not natively support depending .Net version used.

https://blogs.perficient.com/2016/04/28 ... t-support/
SimonT
STHS Owner / Propriétaire du STHS
Posts: 14756
Joined: Sat Oct 08, 2005 4:18 pm
Location: Montreal, Canada
Contact:

Re: Auto-load lines and output via FTP from STHS

Post by SimonT »

I'm running on .Net 2.0 Framework.. But the issue is related to the FTPWebRequest method who I think is even older.
-SimonT
Forum Administrator / Administrateur du Forum
STHS Owner / Propriètaire du STHS
English V2 & V3 Manual - Manuel V2 & V3 Français
JimToupet
STHS Dynamic Website Contributor
Posts: 363
Joined: Wed Jul 02, 2014 10:55 am

Re: Auto-load lines and output via FTP from STHS

Post by JimToupet »

The use of TLS1.2 on server with a connection not supported it can cause an error:
https://stackoverflow.com/questions/408 ... ror-234-au

Seems that MS made TLS1.2 supported on .Net 2.0 :
https://support.microsoft.com/en-nz/hel ... -framework
SimonT
STHS Owner / Propriétaire du STHS
Posts: 14756
Joined: Sat Oct 08, 2005 4:18 pm
Location: Montreal, Canada
Contact:

Re: Auto-load lines and output via FTP from STHS

Post by SimonT »

There is different version of TLS. Does your provider force TLS 1.2 only? Because I did try to run the code using Framework 4.5 and it didn't work either.
-SimonT
Forum Administrator / Administrateur du Forum
STHS Owner / Propriètaire du STHS
English V2 & V3 Manual - Manuel V2 & V3 Français
JimToupet
STHS Dynamic Website Contributor
Posts: 363
Joined: Wed Jul 02, 2014 10:55 am

Re: Auto-load lines and output via FTP from STHS

Post by JimToupet »

I've checked but I don't see any place to change de settings.

Since it's a shared web hosting, it's probably a system wide configuration.
SimonT
STHS Owner / Propriétaire du STHS
Posts: 14756
Joined: Sat Oct 08, 2005 4:18 pm
Location: Montreal, Canada
Contact:

Re: Auto-load lines and output via FTP from STHS

Post by SimonT »

Did you try TLS 1.0 or TLS 1.1?
-SimonT
Forum Administrator / Administrateur du Forum
STHS Owner / Propriètaire du STHS
English V2 & V3 Manual - Manuel V2 & V3 Français
JimToupet
STHS Dynamic Website Contributor
Posts: 363
Joined: Wed Jul 02, 2014 10:55 am

Re: Auto-load lines and output via FTP from STHS

Post by JimToupet »

Since those protocols are deprecated, my hoster don't permit to connect throught those.
JimToupet
STHS Dynamic Website Contributor
Posts: 363
Joined: Wed Jul 02, 2014 10:55 am

Re: Auto-load lines and output via FTP from STHS

Post by JimToupet »

I can test on my side, simply share me the portion of code that have to do with FTP and I'll be able to test.
SimonT
STHS Owner / Propriétaire du STHS
Posts: 14756
Joined: Sat Oct 08, 2005 4:18 pm
Location: Montreal, Canada
Contact:

Re: Auto-load lines and output via FTP from STHS

Post by SimonT »

Et voilà.

Code: Select all

Public Function FtpDirectoryListing(ByVal strSitePathFile As String, ByVal booAnonymous As Boolean, Optional ByVal strUsername As String = "", Optional ByVal strPassword As String = "", Optional ByVal booVerbose As Boolean = True) As List(Of String)
        Try
            FtpDirectoryListing = Nothing
            Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(strSitePathFile), System.Net.FtpWebRequest)
            If booAnonymous = False Then
                clsRequest.Credentials = New System.Net.NetworkCredential(strUsername, strPassword)
            Else
                clsRequest.Credentials = New System.Net.NetworkCredential("Anonymous", "Anonymous")
                'clsRequest.UseDefaultCredentials = True
            End If

            clsRequest.Method = System.Net.WebRequestMethods.Ftp.ListDirectory

            Dim response As System.Net.FtpWebResponse = CType(clsRequest.GetResponse(), System.Net.FtpWebResponse)
            ' Get the stream containing content returned by the server.
            Dim dataStream As IO.Stream = response.GetResponseStream()
            ' Open the stream using a StreamReader for easy access.
            Dim reader As New IO.StreamReader(dataStream)
            ' Read the content.
            Dim responseFromServer As String = reader.ReadToEnd()
            ' Display the content.
            responseFromServer = responseFromServer.Replace(vbCrLf, vbCr).TrimEnd(Chr(13))
            'split the string into a list
            Dim result As New List(Of String)
            result.AddRange(responseFromServer.Split(Chr(13)))
            Return result
        Catch MyError As Exception
            Dim result As New List(Of String)
            Call CatchError(MyError)
            Return result
        End Try
    End Function
    Public Function FtpDownload(ByVal strSitePathFile As String, ByVal strLocalFile As String, ByVal booAnonymous As Boolean, Optional ByVal strUsername As String = "", Optional ByVal strPassword As String = "", Optional ByVal booVerbose As Boolean = True, Optional ByVal booDeleteOriginal As Boolean = False, Optional booNoErrorMessage As Boolean = False) As Boolean
        Try
            If IO.Directory.Exists(ReverseTrim(strLocalFile, "\", True)) = False Then
                If booVerbose = True Then MsgE("FTP Download : " & strLocalFile & " doesn't exist.")
                Return False
            End If

            Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(strSitePathFile), System.Net.FtpWebRequest)
            clsRequest.KeepAlive = False
            If booAnonymous = False Then
                clsRequest.Credentials = New System.Net.NetworkCredential(strUsername, strPassword)
            Else
                clsRequest.Credentials = New System.Net.NetworkCredential("Anonymous", "Anonymous")
                'clsRequest.UseDefaultCredentials = True
            End If
            clsRequest.UseBinary = True
            clsRequest.EnableSsl = True
            clsRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile


            Dim response As System.Net.FtpWebResponse = CType(clsRequest.GetResponse(), System.Net.FtpWebResponse)
            ' Get the stream containing content returned by the server.
            Dim dataStream As IO.Stream = response.GetResponseStream()
            ' Open the stream using a StreamReader for easy access.

            Dim FS As New IO.FileStream(strLocalFile, IO.FileMode.Create)

            Dim buffer(2047) As Byte
            Dim read As Integer = 0
            Do
                read = dataStream.Read(buffer, 0, buffer.Length)
                FS.Write(buffer, 0, read)

            Loop Until read = 0
            dataStream.Close()
            FS.Flush()
            FS.Close()

            If booDeleteOriginal = True Then
                Dim FTPRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(strSitePathFile), System.Net.FtpWebRequest)
                If booAnonymous = False Then
                    FTPRequest.Credentials = New System.Net.NetworkCredential(strUsername, strPassword)
                Else
                    FTPRequest.Credentials = New System.Net.NetworkCredential("Anonymous", "Anonymous")
                    'clsRequest.UseDefaultCredentials = True
                End If
                FTPRequest.Method = System.Net.WebRequestMethods.Ftp.DeleteFile
                Dim DelReponse As System.Net.FtpWebResponse = CType(FTPRequest.GetResponse(), System.Net.FtpWebResponse)
            End If

            If booVerbose = True Then MsgI(strLocalFile & " succesfully downloaded.")
            Return True
        Catch MyError As Exception
            If booNoErrorMessage = False Then MsgE("Note: After a lot of research, I came to the conclusion that I’m unable to support the FTP option for the STHS users. There are 7 different standards (RFC) for FTP. My implementation in the STHS is based on the Microsoft .Net Framework. On the Internet, some FTP servers were created based on 7 different standards. Various compatibility issues exist between them. It’s only normal that some implementations are not compatible with the Microsoft .Net Framework. Sadly, if your FTP server is incompatible with the implement of the STHS, you’ll won't be able to use these options.")
            Call CatchError(MyError)
            Return False
        End Try
    End Function
    Public Function FtpUpload(ByVal strSitePathFile As String, ByVal strLocalFile As String, ByVal booAnonymous As Boolean, Optional ByVal strUsername As String = "", Optional ByVal strPassword As String = "", Optional ByVal booVerbose As Boolean = True, Optional ByVal booDeleteOriginal As Boolean = False, Optional booNoErrorMessage As Boolean = False) As Boolean
        Try
            If IO.File.Exists(strLocalFile) = False Then
                MsgE("FTP Upload - " & strLocalFile & " doesn't exist!")
                Return False
            End If

            Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(strSitePathFile), System.Net.FtpWebRequest)
            clsRequest.KeepAlive = False
            If booAnonymous = False Then
                clsRequest.Credentials = New System.Net.NetworkCredential(strUsername, strPassword)
            Else
                clsRequest.Credentials = New System.Net.NetworkCredential("Anonymous", "Anonymous")
                'clsRequest.UseDefaultCredentials = True
            End If

            clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
            clsRequest.UseBinary = True
            clsRequest.EnableSsl = True

            ' read in file…
            Dim bFile() As Byte = System.IO.File.ReadAllBytes(strLocalFile)

            ' upload file…
            Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
            clsStream.Write(bFile, 0, bFile.Length)

            clsStream.Dispose()
            clsStream.Close()
            If booDeleteOriginal = True Then IO.File.Delete(strLocalFile)
            If booVerbose = True Then MsgI(strLocalFile & " succesfully upload.")
            Return True
        Catch MyError As Exception
            If booNoErrorMessage = False Then MsgE("Note: After a lot of research, I came to the conclusion that I’m unable to support the FTP option for the STHS users. There are 7 different standards (RFC) for FTP. My implementation in the STHS is based on the Microsoft .Net Framework. On the Internet, some FTP servers were created based on 7 different standards. Various compatibility issues exist between them. It’s only normal that some implementations are not compatible with the Microsoft .Net Framework. Sadly, if your FTP server is incompatible with the implement of the STHS, you’ll won't be able to use these options.")
            Call CatchError(MyError)
            Return False
        End Try
    End Function
    Public Function GetFileSize(ByVal MyFilePath As String) As Long
        Dim MyFile As New FileInfo(MyFilePath)
        Dim FileSize As Long = MyFile.Length
        Return FileSize
    End Function
    Public Sub CatchError(ByVal MyError As Exception)
        Dim Spacer As New String(CType("-", Char), 150)
        Spacer = vbNewLine & Spacer & vbNewLine

        Dim strText As New System.Text.StringBuilder
        strText.Append("Exception Type")
        strText.Append(Spacer)
        strText.Append(MyError.GetType().ToString() & vbNewLine & vbNewLine)
        strText.Append("Message - Source")
        strText.Append(Spacer)
        strText.Append(MyError.Message & " - " & MyError.Source & vbNewLine & vbNewLine)
        strText.Append("Stack Trace")
        strText.Append(Spacer)
        strText.Append(MyError.StackTrace)
        Msg(strText.ToString)

    End Sub    
    
-SimonT
Forum Administrator / Administrateur du Forum
STHS Owner / Propriètaire du STHS
English V2 & V3 Manual - Manuel V2 & V3 Français
JimToupet
STHS Dynamic Website Contributor
Posts: 363
Joined: Wed Jul 02, 2014 10:55 am

Re: Auto-load lines and output via FTP from STHS

Post by JimToupet »

Ceci fonctionne chez moi avec mon hébergeur.

Code: Select all

Imports System.Net
Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates

Module Module1

    Public Function FtpDirectoryListing() As List(Of String)
        Dim strSitePathFile As String = "ftp://ftp.example.com"
        Dim strUsername As String = "username"
        Dim strPassword As String = "password"
        Dim booAnonymous As Boolean = False
        Dim UseSSL = True

        Try
            FtpDirectoryListing = Nothing

            Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(strSitePathFile), System.Net.FtpWebRequest)
            If booAnonymous = False Then
                clsRequest.Credentials = New System.Net.NetworkCredential(strUsername, strPassword)
            Else
                clsRequest.Credentials = New System.Net.NetworkCredential("Anonymous", "Anonymous")
                'clsRequest.UseDefaultCredentials = True
            End If

            If UseSSL = True Then
                ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType) 'TLS 1.2
                ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateServerCertificate)
                clsRequest.EnableSsl = True
            End If

            clsRequest.Method = System.Net.WebRequestMethods.Ftp.ListDirectory

            Dim response As System.Net.FtpWebResponse = CType(clsRequest.GetResponse(), System.Net.FtpWebResponse)
            ' Get the stream containing content returned by the server.
            Dim dataStream As IO.Stream = response.GetResponseStream()
            ' Open the stream using a StreamReader for easy access.
            Dim reader As New IO.StreamReader(dataStream)
            ' Read the content.
            Dim responseFromServer As String = reader.ReadToEnd()
            ' Display the content.
            responseFromServer = responseFromServer.Replace(vbCrLf, vbCr).TrimEnd(Chr(13))
            'split the string into a list
            Dim result As New List(Of String)
            result.AddRange(responseFromServer.Split(Chr(13)))
            Return result
        Catch MyError As Exception
            Dim result As New List(Of String)
            Call CatchError(MyError)
            Return result
        End Try
    End Function

    Public Function ValidateServerCertificate(sender As Object, X509Certificate As X509Certificate, X509Chain As X509Chain, SslPolicyErrors As SslPolicyErrors) As Boolean

        ' Refuse self-signed certificate
        If (SslPolicyErrors = SslPolicyErrors.RemoteCertificateChainErrors) Then
            Return False
        End If

        ' Refuse certificate name mismatched maybe not a good test with shared webhosting. 
        'If (SslPolicyErrors = SslPolicyErrors.RemoteCertificateNameMismatch) Then
        ' Return False
        'End If

        Return True
    End Function



End Module
SimonT
STHS Owner / Propriétaire du STHS
Posts: 14756
Joined: Sat Oct 08, 2005 4:18 pm
Location: Montreal, Canada
Contact:

Re: Auto-load lines and output via FTP from STHS

Post by SimonT »

Est-ce que tu sens à l'aise de faire le test dans la version 3.1.5 avec un clé de registre?
-SimonT
Forum Administrator / Administrateur du Forum
STHS Owner / Propriètaire du STHS
English V2 & V3 Manual - Manuel V2 & V3 Français
JimToupet
STHS Dynamic Website Contributor
Posts: 363
Joined: Wed Jul 02, 2014 10:55 am

Re: Auto-load lines and output via FTP from STHS

Post by JimToupet »

Je suis prêt à tester, nous sommes en offseason et j'ai bien hâte que ce pépin soit résolu avec le nombre de mise à jour à faire :)

La clé de registre sert à quoi ?

Tu peux me contacter par courriel
SimonT
STHS Owner / Propriétaire du STHS
Posts: 14756
Joined: Sat Oct 08, 2005 4:18 pm
Location: Montreal, Canada
Contact:

Re: Auto-load lines and output via FTP from STHS

Post by SimonT »

Un clé de registre dans Windows? Tu connais?
-SimonT
Forum Administrator / Administrateur du Forum
STHS Owner / Propriètaire du STHS
English V2 & V3 Manual - Manuel V2 & V3 Français
Post Reply