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