[英]uploading file on secured FTP over https protocol c#
我必須使用HTTPS在FTP上上傳和下載文件。 如果我想的沒錯,由於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();
}
它沒有使用HTTPS; 它正在使用FTPS(或可能的SFTP)。 FtpWebRequest
類通過將EnableSsl
屬性設置為true
支持顯式FTPS。
如果這不起作用,則可能需要使用第三方組件。 看看關於這個SO問題的建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.