访问SFTP站点时出现异常:请求的名称有效,但未找到所请求类型的数据

Bha*_*hah 2 c# sftp socketexception

我是C#的新手,我正在尝试使用C#访问SFTP(从Internet获取一些代码).我通过编写下面的代码尝试了这个.但我得到例外:

IPHostEntry hostInfo = Dns.GetHostByName(@"sftp://........");
// Get the IP address list that resolves to the host names contained in the 
// Alias property.
IPAddress[] address = hostInfo.AddressList;
// Get the alias names of the addresses in the IP address list.
Run Code Online (Sandbox Code Playgroud)

"捕获了SocketException:请求的名称有效,但未找到所请求类型的数据"

我发现了很多关于这一点,但没有理解.此外,我尝试使用Tamir.SharpSSH库连接SFTP,但获得相同的异常.

请建议一些解决方案.我的项目需要这个.

谢谢

Rob*_*Rob 5

我怀疑你需要改变:

IPHostEntry hostInfo = Dns.GetHostByName(@"sftp://server.address.com");
Run Code Online (Sandbox Code Playgroud)

至:

IPHostEntry hostInfo = Dns.GetHostByName(@"server.address.com");
Run Code Online (Sandbox Code Playgroud)

DNS不知道,也不关心您用来访问服务器的协议(sftp://),它只关心服务器名称.

注意:Microsoft认为该Dns.GetHostByName方法已过时,建议您改为使用该Dns.GetHostEntry方法.