我已經構建了一個控制檯應用程序來上傳文件到FTP服務器。由於此FTP服務器僅接受SFTP協議,因此我使用SSH.NET開放源代碼庫,因爲.NET中沒有內置方法(對於.NET 3.5,這是我在Visual Studio 2008 Professional下使用的方法)。使用SSH.NET將文件上傳到SFTP:無法連接
我見過很多例子。我已經實現了一個快速測試應用程序來連接到公共SFTP服務器並使用SFTP協議上傳文件。
我使用下面從here獲得公衆免費SFTP服務器:
server : demo.wftpserver.com
user : demo-user
password : demo-user
port : 2222
這裏是代碼:
const string host = "demo.wftpserver.com";
const string username = "demo-user";
const string password = "demo-user";
const string workingdirectory = "/upload";
const string uploadfile = @"C:\test.txt";
const int port = 2222;
static void Main(string[] args)
{
try
{
using (SftpClient client = new SftpClient(host, port, username, password))
{
{
client.Connect();
Console.WriteLine("Connected to {0}", host);
client.ChangeDirectory(workingdirectory);
Console.WriteLine("Changed directory to {0}", workingdirectory);
var listDirectory = client.ListDirectory(workingdirectory, null);
Console.WriteLine("Listing directory:");
foreach (var fi in listDirectory)
{
Console.WriteLine(" - " + fi.Name);
}
using (var fileStream = new FileStream(uploadfile, FileMode.Open))
{
Console.WriteLine("Uploading {0} ({1:N0} bytes)", uploadfile, fileStream.Length);
client.BufferSize = 4 * 1024; // bypass Payload error large files
client.UploadFile(fileStream, Path.GetFileName(uploadfile), null);
}
}
}
}
catch (Exception e)
{
Console.Out.WriteLine(e.Message);
}
的問題如下:
嘗試連接時與服務器使用以下句子:
client.Connect();
我收到一個異常。唯一的例外說以下內容:
"Value cannot be null. \r\nParameter name: All lists are either null or empty."
StackTrace " at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout)\r\n at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle)\r\n at Renci.SshNet.Session.Connect()\r\n at Renci.SshNet.BaseClient.Connect()\r\n at ConsoleApplication2.Program.Main(String[] args) at C:\\Users\\Win7\\Downloads\\ConsoleApplication2\\ConsoleApplication2\\Program.cs:line 61" string
而且,我試圖改變超時作爲解釋here但沒有成功。
但是,如果我嘗試使用來自FileZilla客戶端的相同憑據連接到相同的SFTP服務器,那麼我可以無問題地連接,甚至可以上傳文件。
所以我不明白髮生了什麼事。有人能幫助我嗎?
UPDATE: 我找到了這個fix。該修補程序已於2017年1月22日在開發支持中應用,但最後下載的二進制文件是2016年12月14日。那麼,如何獲取包含此修復程序的二進制文件?
SharpSSH是一個死的項目。不要使用它。它的網站甚至下降,atm。 –
它還沒死。我們目前在我們的項目中使用它。 –
最後一個版本是從2011年開始的,自2007年以來網頁並沒有更新。這就是我所說的與安全相關的圖書館死亡的內容。它甚至不能支持任何現代密碼。 –