通过https协议在安全的FTP上上传文件c# [英] uploading file on secured FTP over https protocol c#

查看:194
本文介绍了通过https协议在安全的FTP上上传文件c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用HTTPS在FTP上上传和下载文件. 如果我想的没错,由于ftp使用HTTPS协议,则必须使用HTTPWebRequest类.

I have to upload and download files on FTP with HTTPS. If I am thinking right I have to use HTTPWebRequest class since the ftp is using HTTPS protocol.

现在我正在尝试下载文件,但是我得到的所有响应流都只是已登录的虚拟用户'XXX'".

Now I'm trying to download a file but all the response stream I'm getting is just the "virtual user 'XXX' logged in".

可能是我没有设置正确的方法类型,或者其他地方出了错.

May be I'm not setting correct method type or something else is going wrong.

 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();
      }

推荐答案

它不使用HTTPS;它正在使用FTPS(或可能的SFTP).通过将EnableSsl属性设置为trueFtpWebRequest类支持显式FTPS.

It's not using HTTPS; it's using FTPS (or possible SFTP). The FtpWebRequest class supports explicit FTPS by setting the EnableSsl property to true.

如果这不起作用,则可能需要使用第三方组件.看看关于此SO问题的建议.

If that doesn't work, you'll probably need to use a third-party component. Have a look at the suggestions on this SO question.

这篇关于通过https协议在安全的FTP上上传文件c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆