Disasterkid Disasterkid - 4 years ago 326
C# Question

SftpClient using Url instead of IP as host

I am using the following code to upload a file to an

sFTP
server. I have done a test upload using FileZilla and the file is uploaded successfully.

try
{
var client = new SftpClient(host, port, username, password);
client.Connect();
client.ChangeDirectory(workingDir);
var listDirectory = client.ListDirectory(workingDir);
foreach (var file in files)
{
var fileStream = new FileStream(file, FileMode.OpenOrCreate);
client.BufferSize = 4 * 1024; // bypass payload error large files
client.UploadFile(fileStream, Path.GetFileName(file));
Log.Info(string.Format("File [{0}] upload complete",file));
}

}
catch (Exception ex)
{
Log.Error(ex.Message);
}


But in the code above, I cannot seem to get the upload done because I get the following error:


No such host is known


And that's maybe because I am using the Url value of my host instead of IP? (I have done a test with another FtP server using IP and that also seems to be working) I'm wondering if that's the case? If so, is there a way to let
SftpClient
(Ssh.Net) handle the Url?

Answer Source

The host parameter may be an IP address or a host name that can be resolved to an IP address. It may not be a URL, that is something completely different from a technical point of view.

The fact that some full application accepts something else than a host name says little: it splits that string into separat tokens and connects to the IP address it gets returned when resolving the host name. But that does not mean that you can open any network connection to a URL or some arbitrary string. It is only possible to open a connection to an IP address.

So if you successfully test some URL like sftp://ftp.example.com/ContactImport in some "program", this does not mean that you can internally use that string as host parameter in your code. You need to use the host name that is a part of such URL, so ftp.example.com in this case, since only that can be successfully resolved to an IP address.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download