通过https协议C#担保FTP上传文件(uploading file on secured FTP

2019-10-17 08:59发布

我要上传和下载文件的FTP与HTTPS。 如果我想正确的我,因为FTP使用HTTPS协议使用HttpWebRequest类。

现在,我想下载一个文件,但所有我得到的响应流仅仅是“虚拟用户‘XXX’登录”。

可能是我没有设置正确的方法类型或别的东西是怎么了。

 WebRequest ftp1 = HttpWebRequest.Create(u1);
            ftp1.PreAuthenticate = true;
            ftp1.Credentials = netCred;

            ftp1.Method = WebRequestMethods.Ftp.DownloadFile;
            try
            {
                response = (HttpWebResponse)ftp1.GetResponse();            
                remoteStream = response.GetResponseStream();
                localStream = File.Create("c:/TEST1.txt");
                int bytesProcessed = 0;
                byte[] buffer = new byte[4096];
                int bytesRead;

                do
                {
                    // Read data (up to 1k) from the stream
                    bytesRead = remoteStream.Read(buffer, 0, buffer.Length);

                    // Write the data to the local file
                    localStream.Write(buffer, 0, bytesRead);

                    // Increment total bytes processed
                    bytesProcessed += bytesRead;
                } while (bytesRead > 0);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
      finally
      {
        // Close the response and streams objects here 
        // to make sure they're closed even if an exception
        // is thrown at some point
        if (response != null) response.Close();
        if (remoteStream != null) remoteStream.Close();
        if (localStream != null) localStream.Close();
      }

Answer 1:

它不使用HTTPS; 它的使用FTPS(或可能的SFTP)。 该FtpWebRequest类支持通过设置明确的FTPS EnableSsl属性为true

如果不工作,你可能需要使用第三方组件。 看看上建议, 这太问题 。



文章来源: uploading file on secured FTP over https protocol c#