2017-04-11 160 views
0

我已經構建了一個控制檯應用程序來上傳文件到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日。那麼,如何獲取包含此修復程序的二進制文件?

回答

1

我已經解決了。

我從開發人員分支中下載了此錯誤已更正的源代碼。然後,我在Visual Studio 2015中打開.NET Framework 3.5項目,然後構建它。所以我獲得了二進制文件,用於發佈和調試的DLL。然後我回到我的Visual Studio 2008項目,並添加了這個參考。我已經使用相同的代碼檢查過了,我可以使用相同的憑據連接到FTP服務器。

SSH.NET web提供的最後一個二進制文件不包含修正的這個bug。

-3

我使用Tamir.SharpSSH在我們的SFTP中發送文件。嘗試使用這一個,下面的代碼是發送文件到你的sftp最簡單的方法。

Sftp sftpBase = new Tamir.SharpSsh.Sftp(_sftpHost, _sftpUid, Up_decryptPass); 
sftpBase.Connect(int.Parse(_sftpPort)); 
sftpBase.Put(Dir + filename, sftpDir); 
+1

SharpSSH是一個死的項目。不要使用它。它的網站甚至下降,atm。 –

+0

它還沒死。我們目前在我們的項目中使用它。 –

+0

最後一個版本是從2011年開始的,自2007年以來網頁並沒有更新。這就是我所說的與安全相關的圖書館死亡的內容。它甚至不能支持任何現代密碼。 –