Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. public static bool AcceptAllCertificatePolicy(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  2. {
  3. return true;
  4. }
  5.  
  6. public static string Upload_SSL(string filenameSrc)
  7. {
  8.  
  9. ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertificatePolicy;
  10.  
  11. FileInfo fileInfSrc = new FileInfo(filenameSrc);
  12. FtpWebRequest reqFTP;
  13.  
  14. // Create FtpWebRequest object from the Uri provided
  15. if (strDirectory.Trim() != "")
  16. {
  17. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strHost.Trim() + "/" + strDirectory.Trim() + "/" + fileInfSrc.Name.Trim()));
  18. }
  19. else
  20. {
  21. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strHost.Trim() + "/" + fileInfSrc.Name.Trim()));
  22. }
  23.  
  24. // Provide the WebPermission Credintials
  25. reqFTP.Credentials = new NetworkCredential(strUser.Trim(), strPass.Trim());
  26.  
  27. reqFTP.EnableSsl = true;
  28.  
  29. // Test Fabio du 15/01/2013
  30. reqFTP.Proxy = null;
  31.  
  32. // By default KeepAlive is true, where the control connection is not closed
  33. // after a command is executed.
  34. reqFTP.KeepAlive = false;
  35.  
  36. // Specify the command to be executed.
  37. reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
  38.  
  39. // Specify the data transfer type.
  40. reqFTP.UseBinary = true;
  41.  
  42. // Notify the server about the size of the uploaded file
  43. reqFTP.ContentLength = fileInfSrc.Length;
  44.  
  45. // The buffer size is set to 8kb
  46. int buffLength = 8192;
  47. byte[] buff = new byte[buffLength];
  48. int contentLen;
  49.  
  50. // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
  51. FileStream fs = fileInfSrc.OpenRead();
  52.  
  53. try
  54. {
  55. // Stream to which the file to be upload is written
  56. Stream strm = reqFTP.GetRequestStream();
  57.  
  58. // Read from the file stream 2kb at a time
  59. contentLen = fs.Read(buff, 0, buffLength);
  60.  
  61.  
  62. // Till Stream content ends
  63. while (contentLen != 0)
  64. {
  65. // Write Content from the file stream to the FTP Upload Stream
  66. strm.Write(buff, 0, contentLen);
  67. contentLen = fs.Read(buff, 0, buffLength);
  68. }
  69.  
  70. // Close the file stream and the Request Stream
  71. strm.Close();
  72. fs.Close();
  73. }
  74. catch (Exception ex)
  75. {
  76. fs.Close();
  77. return (ex.Message);
  78. }
  79.  
  80. return "ok";
  81. }
  82.  
  83. myFtp.Class1.strHost = "ftp://XXXXXXXXXXXXX";
  84. myFtp.Class1.strPass = "*****************";
  85. myFtp.Class1.strUser = "*********";
  86. myFtp.Class1.nPort = 21;
  87. myFtp.Class1.Upload_SSL(@"D:Test.txt");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement