c# - 使用 SSH.NET 在端口 990 上连接到 FTP 站点时出现 "The server response contains a null character"

标签 c# ssl ftp sftp ssh.net

我正在尝试使用 Renci SSH.NET 连接到 FTP 站点并从我的 C# 应用程序下载文件。我可以使用 FileZilla 和 WinSCP(在 Windows 10 Pro 64 上)以及通过 HTTP 连接(主机正在运行 CrushFTP)成功连接到主机,但无法弄清楚如何使用 SSH.NET 从我的 C# 应用程序成功连接.我收到此错误:

The server response contains a null character at position 0x00000004: 00000000 15 03 03 00....A server must not send a null character before the Protocol Version Exchange is complete.


我对 here 的文档的看法,是这个问题似乎是一个预期的错误。我已阅读 https://www.rfc-editor.org/rfc/rfc4253#section-4.2 上的文档建议的信息我“认为”我理解这意味着我尝试连接的主机可能没有正确配置。我的问题是,由于我无法更改主机的配置方式,是否有办法通过 SSH.NET 库解决此错误,或者我只是在使用 SSH.NET 时处于死胡同?
这是我的代码 - 试图简化到最低限度:
using System;
using Renci.SshNet;

namespace mysftp
{
    class Program
    {
        static void Main(string[] args)
        {
            string host = "myftps.hostname.com";
            string username = "myusername";
            string password = "securepassword";
            int port = 990;

            string remoteFolder = "/";

            using (SftpClient sftp = new SftpClient(host, port, username, password)) 
            {
                try
                {
                    Console.WriteLine("Attempting to connect to " + host + " ...");
                    sftp.Connect();
                    Console.WriteLine(sftp.ConnectionInfo.ServerVersion);
                    sftp.Disconnect();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
        }
    }
}
这是主机信息(来自 WinSCP 的远程服务器信息):
Remote system = UNIX Type: L8
File transfer protocol = FTP
Cryptographic protocol = TLS/SSL Implicit encryption, TLSv1.2
Encryption algorithm = TLSv1.2: ECDHE-RSA-AES256-GCM-SHA384, 2048 bit RSA, ECDHE-RSA-AES256-GCM-SHA384 TLSv1.2 Kx=ECDH     Au=RSA  Enc=AESGCM(256) Mac=AEAD

Compression = No
------------------------------------------------------------
Certificate fingerprint
<<fingerprint info redacted>>
------------------------------------------------------------
Can change permissions = Yes
Can change owner/group = No
Can execute arbitrary command = Protocol commands only
Can create symbolic/hard link = No/No
Can lookup user groups = No
Can duplicate remote files = No
Can check available space = No
Can calculate file checksum = Yes
Native text (ASCII) mode transfers = No
------------------------------------------------------------
Additional information
The server supports these FTP additional features:
  AUTH TLS
  AUTH SSL
  SSCN
  PBSZ
  PROT
  CCC
  CLNT
  EPSV
  EPRT
  MDTM
  MDTM YYYYMMDDHHMMSS[+-TZ];filename
  MFMT
  SIZE
  REST STREAM
  MODE Z
  LIST -Q
  SITE UTIME
  SITE MD5
  XMD5
  SITE MD5s
  SITE RANDOMACCESS
  MLST Type*;Size*;Modify*;Perm*;UNIX.owner*;UNIX.group*;
  UTF8

最佳答案

端口 990 用于通过隐式加密 TLS/SSL 连接(又名隐式 FTPS)的 FTP 协议(protocol)。而 SSH.NET 是一个 SSH/SFTP 库。 FTPS 和 SFTP 是完全不同的东西。您不能将 SSH.NET 用于 FTPS。
隐式 FTPS 已过时。并非所有 FTP(S) 库都支持它。值得注意的是,内置的 .NET FTP 实现不支持 FtpWebRequest .
因为您将不得不使用第 3 方库。见 Does .NET FtpWebRequest Support both Implicit (FTPS) and explicit (FTPES)?

尽管大多数 FTP 服务器都支持标准的显式 FTPS(通过端口 21)。如果您的服务器有,请使用它,而不是旧端口 990。请参阅 FTPS (FTP over SSL) in C#
如果没有,请与服务器管理员联系以启用它。

另见 Difference between FTP/FTPS/SFTP - Configurable connection to any of them

可能导致相同错误消息的完全不同的问题:Connecting with SSH.NET to OpenSSH 7.4p1 fails with "The server response contains a null character at position" but works in WinSCP .

关于c# - 使用 SSH.NET 在端口 990 上连接到 FTP 站点时出现 "The server response contains a null character",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65764847/

相关文章:

ant - 通过 TLS 的 Apache Ant FTP 连接

c# - 无法在 dotnet 核心 Entity Framework 中导入 OleDbConnection

c# - 如何确定 SSL 连接的 "strength"

C# 扩展 (OmniSharp) 在 VSCode 中给出错误结果

c# - 为什么我不能转换 ref 参数?

ssl - 使用haproxy的http请求到https请求

ssl - asp.net core 将 http 流量重定向到 https 问题

在 IPhone/ATT 上进行网络共享时出现 Python FTP SSL 错误

时间:2019-05-17 标签:c#socketconnectaddressfamily

xcode - cocoa 中的 gnuTLS 或 SSL