1

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();
      }
user1810332
  • 11
  • 1
  • 4

1 Answers1

0

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.

Community
  • 1
  • 1
Richard Deeming
  • 29,830
  • 10
  • 79
  • 151
  • I tried using with FTPWebrequest but it failed because of URL I provide i.e. HTTPS://secureftp.abc.com. – user1810332 Nov 09 '12 at 13:57
  • Richard is right, I think you need to clarify with someone that providing you the URL if that's FTP(Secure) or Http(Secure). – Turbot Nov 09 '12 at 14:13
  • When you have a FTP over HTTPS (HTTP with SSL), what should it be called? – user1810332 Nov 09 '12 at 14:17
  • There's no "FTP over HTTPS" protocol. You can either have FTP over SSL, which is FTPS, or SSH File Transfer Protocol, which is SFTP. http://www.eldos.com/sbb/sftp-ftps.net/ftps-vs-sftp.php – Richard Deeming Nov 09 '12 at 14:19
  • You must be right but I'm using a site to upload/download files over HTTPS. As I have given the part of URL earlier too... https://secureftp.abc.com. I have read many blogs and articles about FTPS and SFTP but my vendor guidleines is confusing me. – user1810332 Nov 09 '12 at 14:29
  • I still don't know how to use that URL with FTPWebRequest. – user1810332 Nov 09 '12 at 15:13
  • You cannot use an HTTPS URL with the `FtpWebRequest` class; you will have to use an FTP URL for FTP, or an `HttpWebRequest` for HTTPS. From the description of the problem, you're connecting to an FTP server, so you'll need to use an FTP URL. – Richard Deeming Nov 09 '12 at 16:30