FTPS加密上传

公司要求ftp接口不能以明文方式传输,所以adc系统将增加ftps方式 但是在网找了很多方式都无法实现
用了
方法一

  FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FileUploadPath);
  request.Method = WebRequestMethods.Ftp.ListDirectory;
  request.Credentials = new NetworkCredential(UpLoadUser,UpLoadPWD);

  request.EnableSsl = true;
  request.UsePassive = true;
  ServicePointManager.ServerCertificateValidationCallback = delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)  
  { return true; };

  FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  Stream responseStream = null;
  StreamReader readStream = null;
  try
  {
  responseStream = response.GetResponseStream();
  readStream = new StreamReader(responseStream, System.Text.Encoding.UTF8);
  if (readStream != null)
  Console.WriteLine(readStream.ReadToEnd());
  }
  finally
  {
  if (readStream != null)
  readStream.Close();
  if (response != null)
  response.Close();
  }   

方法二
  public void Upload(string filename)
  {
    
  FileInfo fileInf = new FileInfo(filename);
  string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
  FtpWebRequest reqFTP;

  // 根据uri创建FtpWebRequest对象  
  reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));

  // ftp用户名和密码  
  reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
  //SSL
  reqFTP.EnableSsl = true;
  // 默认为true,连接不会被关闭  
  // 在一个命令之后被执行  
  reqFTP.KeepAlive = false;
    
  // 指定执行什么命令  
  reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

  // 指定数据传输类型  
  reqFTP.UseBinary = true;
  //指定传输模式
  reqFTP.UsePassive =false;

  // 上传文件时通知服务器文件的大小  
  reqFTP.ContentLength = fileInf.Length;

  // 缓冲大小设置为2kb  
  int buffLength = 2048;

  byte[] buff = new byte[buffLength];
  int contentLen;

  // 打开一个文件流 (System.IO.FileStream) 去读上传的文件  
  FileStream fs = fileInf.OpenRead();
  try
  {
  // 把上传的文件写入流  
  Stream strm = reqFTP.GetRequestStream();

  // 每次读文件流的2kb  
  contentLen = fs.Read(buff, 0, buffLength);

  // 流内容没有结束  
  while (contentLen != 0)
  {
  // 把内容从file stream 写入 upload stream  
  strm.Write(buff, 0, contentLen);

  contentLen = fs.Read(buff, 0, buffLength);
  }

  // 关闭两个流  
  strm.Close();
  fs.Close();
  }
  catch (Exception ex)
  {
  // MessageBox.Show(ex.Message, "Upload Error");
  }
  }

都是报远程链接失败 另外提一下 我们的服务器是不需要校验证书的 端口号是990 一定要是被动模式才能登录
麻烦各位大虾 帮忙看看有什么改进的办法 或者有直接的案例 提供一下

原文地址:https://www.cnblogs.com/feige/p/5554342.html