Results 1 to 4 of 4

Thread: FTP File Transfer

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    15

    FTP File Transfer

    I am trying to access a FTP secured site (SSL/TSL) and transfer a file in VB.Net using FTPRequest. When I try to complete the transfer I get an error back "The remote certificate is invalid according to the validation procedure".

    However when I connect to the same FTP site with FileZilla UI, I can transfer files with no problem. Why can do transfers with FileZilla and not in VB.Net? Does FileZilla have its own Certificate?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: FTP File Transfer

    Can you show us the code you're using?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2008
    Posts
    15

    Re: FTP File Transfer

    here is the code: I have replace the siteid, userid and password from the code.

    Dim URI As String = "FTP://<ftpsite.com>"
    Dim UserName As String = "<userid>"
    Dim Password As String = "<userpsw>"

    Dim WResp As FtpWebResponse
    Dim Wreq As FtpWebRequest = DirectCast(FtpWebRequest.Create(URI & "/test.txt"), FtpWebRequest)
    Try

    Wreq.EnableSsl = True
    Wreq.KeepAlive = True
    Wreq.UsePassive = True

    Wreq.Method = WebRequestMethods.Ftp.UploadFile
    Wreq.Credentials = New System.Net.NetworkCredential(UserName, Password)
    Dim bFile() As Byte = System.IO.File.ReadAllBytes("f:\ftpud.txt")

    ' upload file...
    ****** it is here when I try the GetRequestStream that I get the error *****
    Dim clsStream As System.IO.Stream = Wreq.GetRequestStream()

    clsStream.Write(bFile, 0, bFile.Length)
    clsStream.Close()
    clsStream.Dispose()
    WResp = Wreq.GetResponse()

    Catch ew As WebException
    MsgBox(ew.Message)
    Catch e As Exception
    MsgBox(e.Message)
    End Try

  4. #4
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: FTP File Transfer

    You need to register a callback that performs the certificate lookup/validation. I'm afraid I only have a C# example but something like:

    Code:
                    // Register a callback function to validate the server's X509
                    // certificate for SSL
                    System.Net.ServicePointManager.ServerCertificateValidationCallback =
                   new RemoteCertificateValidationCallback(ValidateServerCertificate);
    where the callback function is:-
    Code:
            /// <summary>
            /// Validates a servers X509 certificate
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="certificate"></param>
            /// <param name="chain"></param>
            /// <param name="sslPolicyErrors"></param>
            /// <returns></returns>
            public static bool ValidateServerCertificate(object sender,
                    X509Certificate certificate, 
                    X509Chain chain,
                    SslPolicyErrors sslPolicyErrors)
            {
                if (sslPolicyErrors ==
                   SslPolicyErrors.RemoteCertificateChainErrors)
                {
    
                    return false;
                }
                else if (sslPolicyErrors ==
                   SslPolicyErrors.RemoteCertificateNameMismatch)
                {
    
    
                    System.Security.Policy.Zone z =
                       System.Security.Policy.Zone.CreateFromUrl
                       (((WebRequest)sender).RequestUri.ToString());
                    if (z.SecurityZone ==
                       System.Security.SecurityZone.Intranet ||
                       z.SecurityZone ==
                       System.Security.SecurityZone.MyComputer)
                    {
    
                        return true;
                    }
    
                    return false;
                }
                return true;
            }

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