【问题标题】:Issue with download a file from FTP Server从 FTP 服务器下载文件的问题
【发布时间】:2018-01-21 00:47:17
【问题描述】:

每当我尝试从这个 ftp 服务器下载文件时,我都会收到此错误:

System.Net.WebException: 'The remote server returned an error: 227 Entering Passive Mode (37,60,247,183,135,216)

public static main(string[] args){

    FtpWebRequest ftpRequest = null;
    FtpWebResponse ftpResponse = null;
    ftpRequest = (FtpWebRequest)FtpWebRequest.Create("ftp://ftp.autoherosrpg.com/hash.xml");
    ftpRequest.Credentials = new NetworkCredential("game@autoherosrpg.com", "*********");
    ftpRequest.UseBinary = true;
    ftpRequest.UsePassive = true;
    ftpRequest.KeepAlive = false;
    ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
    ftpResponse = (FtpWebResponse) ftpRequest.GetResponse();
}

但即使我将“UsePassive”切换为 false,我也会得到:

public static main(string[] args){

    FtpWebRequest ftpRequest = null;
    FtpWebResponse ftpResponse = null;
    ftpRequest = (FtpWebRequest)FtpWebRequest.Create("ftp://ftp.autoherosrpg.com/hash.xml");
    ftpRequest.Credentials = new NetworkCredential("game@autoherosrpg.com", "*********");
    ftpRequest.UseBinary = true;
    ftpRequest.UsePassive = false;
    ftpRequest.KeepAlive = false;
    ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
    ftpResponse = (FtpWebResponse) ftpRequest.GetResponse();
}

System.Net.WebException: 'The remote server returned an error: (500) Syntax error, command unrecognized.'

这是我第一次尝试通过 C# 连接到 ftp 服务器。此连接适用于任何 ftp 客户端和浏览器

更新: FTP 服务器目前不支持 TLS。一旦我让这段代码在没有加密的服务器上工作,我将修改代码以使用 TLS。

FTP Log

【问题讨论】:

  • 您能否使用运行 C# 代码的同一台计算机上的任何独立 FTP 客户端连接到 FTP 服务器?向我们展示它的日志文件。
  • 是的,我可以。我在帖子的最后说过,如果我厌倦了任何 ftp 客户端或浏览器,我可以连接到 ftp 服务器。我可以发布日志文件
  • 那是无用的日志。它不显示 FTP 协议脚本。它是服务器端日志吗?向我们展示详细客户端日志。

标签: c# ftp


【解决方案1】:

如您所说,当您切换到“UsePassive”时,它会返回“语法错误,命令无法识别”。这意味着你在某处有错误的语法,因为你没有在这里公开完整的源代码,所以我无法弄清楚。

【讨论】:

  • 这是我添加到 Program 类中的静态 main 方法中的唯一代码,它是在 Visual Studio 中创建控制台项目时创建的。出于安全原因,我没有公开用户名和密码,但它们是字符串。我已经更新了上面的代码以反映这一点。