C # Download data from FTPS-based FTP server (use of ftpwebrequest) SSL encryption

Source: Internet
Author: User
Tags ftp protocol

FTPS, or ftpes, is an extension of the FTP protocol for support for TLS and SSL protocols. This article describes how to download an instance of data from a FTPS-based server.  any place, if there is a mistake, you are welcome to advise friends.  not many words, on the yards.
1 usingSystem;2 usingSystem.Net;3 usingSystem.IO;4 usingSystem.Net.Security;5 usingSystem.Security.Cryptography.X509Certificates;6 7 namespaceFtpwebrequestblogcase8 {9     class ProgramTen     { One         Static voidMain (string[] args) A         { -DownLoadFile ("Ftp://xxx/xxx.zip"); - Console.readkey (); the         } -  -          Public Static voidDownLoadFile (stringaddr) -         { +             varreq =(FtpWebRequest) webrequest.create (addr); -Req. Method =WebRequestMethods.Ftp.DownloadFile; +Req. Usebinary =true; AReq. Usepassive =true; at             Const stringUserName ="xxxx"; -             Const stringPassword ="xxxx"; -Req. Credentials =Newnetworkcredential (userName, password); -  -             //If the FTP server you are connecting to requires credentials and supports Secure Sockets Layer (SSL), you should set Enablessl to true.  -             //421 Error if not written (Service unavailable) inReq. Enablessl =true; -  to             //The first time you connect to an FTP server, there is a certificate allocation process.  +             //If none of the following code will report an exception: -             //System.Security.Authentication.AuthenticationException: The remote certificate is not valid according to the verification process.  theServicepointmanager.servercertificatevalidationcallback = *                 NewRemotecertificatevalidationcallback (validateservercertificate); $             TryPanax Notoginseng             { -                 using(varres =(FtpWebResponse) req. GetResponse ()) the                 { +                     Const stringLocalFile ="Test.zip"; A                     varFS =NewFileStream (LocalFile, FileMode.Create, FileAccess.Write); the                     Const intBuffer =1024x768* +; +                     varb =New byte[buffer]; -                     vari =0; $                     varstream =Res. GetResponseStream (); $                      while(Stream! =NULL&& (i = stream. Read (b,0, buffer)) >0) -                     { -Fs. Write (b,0, i); the FS. Flush (); -                     }Wuyi                 } theConsole.WriteLine ("done!"); -  Wu             } -             Catch(Exception ex) About             { $                 varMessage =Ex. ToString (); - Console.WriteLine (message); -             } -             finally A             { +  the             } -         } $  the          Public Static BOOLvalidateservercertificate the(Objectsender, X509Certificate certificate, X509chain chain, sslpolicyerrors sslpolicyerrors) the         { the             return true; -         } in     } the}
View Codein fact, more than the normal FTP download file function more than two lines of code:1 req.  Enablessl = true; The FTPs-based FTP server requires credentials and supports Secure Sockets Layer (SSL), so it needs to be set to true.   2 servicepointmanager.servercertificatevalidationcallback =new Remotecertificatevalidationcallback (validateservercertificate); Public static bool Validateservercertificate (object sender, X509Certificate certificate, X509chain chain, SslP Olicyerrors sslpolicyerrors)
        {
return true;
        }The certificate validation process. The next article will explore how to encapsulate the FtpWebRequest class to make it more robust and easy to use.

C # Download data from FTPS-based FTP server (FtpWebRequest use) SSL encryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.