Scp on WebClient class in .Net

I have a project already written using the .Net WebClient class. It works fine for FTP and WebDAV resources, but how can I get it to work with SCP or SFTP?

+2


source to share


2 answers


... Framework.Net does not contain built-in SCP support; try SharpSSH .



The FtpWebRequest class supports FTPES by setting EnableSsl , but WebClient does not expose it, so you will need to use FtpWebRequest directly.

+1


source


You can make WebClient work with FTP / SSL (but not SFTP) by registering your own prefix:

private void RegisterFtps()
{
    WebRequest.RegisterPrefix("ftps", new FtpsWebRequestCreator());
}

private sealed class FtpsWebRequestCreator : IWebRequestCreate
{
    public WebRequest Create(Uri uri)
    {
        FtpWebRequest webRequest = (FtpWebRequest)WebRequest.Create(uri.AbsoluteUri.Remove(3, 1)); // Removes the "s" in "ftps://".
        webRequest.EnableSsl = true;
        return webRequest;
    }
}

      



Once you've done that, you can use WebRequest almost as usual, except that your URIs begin with "ftps: //" instead of "ftp: //". One caveat is that you must specify a method as it won't be the default. For example.

// Note here that the second parameter can't be null.
webClient.UploadFileAsync(uploadUri, WebRequestMethods.Ftp.UploadFile, fileName, state);

      

+1


source







All Articles