“SocketException: Could not resolve host” when connecting to SFTP server with SSH.NET

走远了吗. 提交于 2020-06-08 13:15:52

问题


Trying to upload file to SFTP server in my local. Hit error when connect SFTP.

System.Net.Sockets.SocketException: Could not resolve host 'sftp://localhost'

I've tried using command-line and FileZilla to connect. It can connected.

string host = "sftp://localhost";
int port = 22;
string username = "gth";
string password = "1234";
using (SftpClient client = new SftpClient(host, port, username, password))
{
    client.Connect();
}

It show me error below:

Loaded assembly: System.Threading.Tasks.dll [External] 
Thread started: <Thread Pool> #4
Thread started: <Thread Pool> #5
Thread started: <Thread Pool> #6
Thread started: <Thread Pool> #7
Unhandled Exception:
01-09 11:47:25.081 E/mono    (12159): 
01-09 11:47:25.081 E/mono    (12159): Unhandled Exception:
01-09 11:47:25.081 E/mono    (12159): System.Net.Sockets.SocketException (0x80004005): Connection refused
01-09 11:47:25.081 E/mono    (12159):   at (wrapper dynamic-method) System.Object.5(intptr,intptr,intptr)
01-09 11:47:25.081 E/mono-rt (12159): [ERROR] FATAL UNHANDLED EXCEPTION: System.Net.Sockets.SocketException (0x80004005): Connection refused
01-09 11:47:25.081 E/mono-rt (12159):   at (wrapper dynamic-method) System.Object.5(intptr,intptr,intptr)
01-09 11:47:25.099 E/CameraBase(12159): SCANNING dlclose for instance 0x437bbb5ee79d6b9f
01-09 11:47:25.100 E/CameraBase(12159): SCANNING AFTER DLCLOSE

回答1:


The host argument of SftpClient constructor takes hostname, not URL.

So just drop the sftp://:

host = "localhost";


来源:https://stackoverflow.com/questions/54102112/socketexception-could-not-resolve-host-when-connecting-to-sftp-server-with-ss

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!