我必须使用 HTTPS 在 FTP 上上传和下载文件。如果我想对了,我必须使用 HTTPWebRequest 类,因为 ftp 使用的是 HTTPS 协议。
现在我正在尝试下载一个文件,但我得到的所有响应流只是“登录的虚拟用户'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();
}