Sunday, April 26, 2009

Upload files on Secure FTP

hi friends
i am having trouble with the Secure ftp site. i was not able to upload the files on the secure ftp site using my code. the code works good if the ftp site don't have the Secure SSI connection. then i have written a code which uses FtpWebRequest for uploading the data on secure ftp site. In this code i have one problem about the Certificates which should be issued in order to make it secure sit. i don't have that certificates so i have written a static method which will make the certification value as valid in any case. here is the code. hope it will help you guys
public bool SecureConnection(string UrlPath, string UserName, string Password,string command,string filePath,string fileName) { try { //String serverUri = "ftp://ftpsite.com/folder1/Test"; if (command.Equals("UF")) UrlPath = UrlPath + "//" + fileName; //String serverUri =UrlPath; FtpWebRequest request = (FtpWebRequest)WebRequest.Create(UrlPath); request.Credentials = new NetworkCredential(UserName, Password); request.UsePassive = true; request.EnableSsl = true; switch (command) { case "MD": if(SecureConnectionRemoveDirectory(UrlPath,UserName,Password)) request.Method = WebRequestMethods.Ftp.MakeDirectory; break; case "UF": request.Method = WebRequestMethods.Ftp.UploadFile; StreamReader sourceStream = new StreamReader(filePath); byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); sourceStream.Close(); request.ContentLength = fileContents.Length; Stream requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); requestStream.Close(); break; } ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); } catch (Exception _exp) { return false; } return true; } public static bool ValidateServerCertificate(object sender,X509Certificate certificate, X509Chain chain,SslPolicyErrors sslPolicyErrors) { return true; }

No comments: