0

我们最近将工作中的 ftp 站点从ftp://address切换到https://ftp.address,现在我检索文件的应用程序无法下载具有预期内容的文件。

我以前使用过 FtpWebRequest,它已经工作了很多年。我现在正在尝试使用 WebClient 并下载一个文件,但它不是我需要的文本文件。相反,文件的内容变成了一些 HTML。

以前的工作:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(getAppSetting("URI") + getAppSetting("FilePath") + args[0].ToString());

request.Method = WebRequestMethods.Ftp.DownloadFile;

setCredentials(request);

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

Changed code to handle https:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

WebClient request = new WebClient();                  

setCredentials(request);

string file = request.DownloadString(getAppSetting("URI") + getAppSetting("FilePath") + args[0].ToString());

结果是一个已下载但包含 HTML 的文件。我希望在我从中提取的网站上拥有文件的内容。

4

1 回答 1

1

经过几周的研究,我终于找到了我需要做的事情,答案很简单。我发现我们的新网站使用 SSH,所以我通过我的 nugent 包将 SSH.Net 安装到 Visual Studio,并添加了“使用 Renci.SshNet;”。我使用以下代码将文件下载到本地驱动器:

using (SftpClient sftp = new SftpClient("ftp.website.com", 22,"username", "password")
{
   sftp.Connect();

   if (sftp.IsConnected)
   {
     using (Filestream fs = new FileStream("Output.txt", FileMode.Create))
      {
        sftp.DownloadFile("/file/Client/Input.txt", fs);
      }
   } 

   sftp.Disconnect();
 }

这完美地工作。我的文件不再作为 HTML 文件下载,我得到了我想要的输出。我希望这可以节省其他人很多时间。

于 2019-07-31T19:21:57.613 回答