小编Ris*_*mar的帖子

使用 SSH.NET 恢复网络连接后自动恢复 SFTP 下载

我正在使用 SSH.NET 库来创建 SFTP 客户端。如果网络连接在此超时内再次可用,我需要恢复下载。我正在使用下面提到的方法,如许多示例所示。

PrivateKeyFile ObjPrivateKey = new PrivateKeyFile(keyStream);
PrivateKeyAuthenticationMethod ObjPrivateKeyAutentication = new PrivateKeyAuthenticationMethod(username, ObjPrivateKey);

var connectionInfo = new ConnectionInfo(hostAddress, port, username, ObjPrivateKeyAutentication);

try
{
    using (var client = new SftpClient(connectionInfo))
    {
        client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(10);

        client.Connect();

        if (!client.IsConnected)
        {
            return false;
        }

        if (!client.Exists(source))
        {
            return false;
        }

        var fileName = Path.GetFileName(source);

        using (var fs = new FileStream(destination + fileName, FileMode.Create))
        {
            client.DownloadFile(source, fs, printActionDel);
            fs.Close();
            returnState = true;
        }

        client.Disconnect();
        client.Dispose();
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在拔下网线以中断下载并测试超时情况。虽然我在超时内再次启用互联网连接以恢复下载,但它并没有恢复。我在这里做错了什么?请指教。

.net c# sftp ssh.net

5
推荐指数
1
解决办法
3264
查看次数

标签 统计

.net ×1

c# ×1

sftp ×1

ssh.net ×1