2012-11-08 96 views
1

我必须使用HTTPS上传和下载FTP文件。 如果我正确思考,我必须使用HTTPWebRequest类,因为ftp使用的是HTTPS协议。通过https协议在安全FTP上传文件#

现在我试图下载一个文件,但是我得到的所有响应流只是登录的“虚拟用户'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(); 
     } 
+0

你能把代码放在这里吗? – Turbot

+0

我已添加基本代码。我刚刚开始了解它。 – user1810332

回答

0

它没有使用HTTPS;它使用FTPS(或可能的SFTP)。通过将EnableSsl属性设置为trueFtpWebRequest类支持显式FTPS。如果这不起作用,您可能需要使用第三方组件。看看this SO question上的建议。

+0

我尝试使用FTPWebrequest,但因为我提供的URL(例如HTTPS://secureftp.abc.com)而失败。 – user1810332

+0

理查德是对的,我想你需要澄清与某人提供您的URL,如果这是FTP(安全)或Http(安全)。 – Turbot

+0

当您通过HTTPS进行FTP(带SSL的HTTP)时,应该调用什么? – user1810332