uploading file on secured FTP over https protocol c#

此生再无相见时 提交于 2019-12-11 05:54:01

问题


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.

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

回答1:


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.

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://stackoverflow.com/questions/13296765/uploading-file-on-secured-ftp-over-https-protocol-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!