.NET Framework Bookmark and Share   
 index > .NET Framework Networking and Communication > How to change the MS-FTP sample from FTP to SFTP
 

How to change the MS-FTP sample from FTP to SFTP

Hi,

The FTPSample belowfrom Microsoft is very helpfull to get my application to send a file via FTP to a server.

Now our company is moving from FTP to SFTP and I need to update my application to use not only FTP but also SFTP.

I don't want to use any (paid) 3rd party applications.

I did find the FtpWebRequest.EnableSsl Property but there must be something more than just setting this property to true.

Can someone explain me how to change or what to add tothe current MS-FTPSample in VB so that I can also send via SFTP?

Thank you.

Micha.

This is the MS sample for FTP which I need to get on SFTP:

Private Sub Upload(ByVal fileName As String, ByVal uploadUrl As String)

Dim requestStream As Stream = Nothing

Dim fileStream As FileStream = Nothing

Dim uploadResponse As FtpWebResponse = Nothing

Try

Dim uploadRequest As FtpWebRequest = WebRequest.Create(uploadUrl)

uploadRequest.Method = WebRequestMethods.Ftp.UploadFile

' UploadFile is not supported through an Http proxy

' so we disable the proxy for this request.

uploadRequest.Proxy = Nothing

requestStream = uploadRequest.GetRequestStream()

fileStream = File.Open(fileName, FileMode.Open)

Dim buffer(1024) As Byte

Dim bytesRead As Integer

While True

bytesRead = fileStream.Read(buffer, 0, buffer.Length)

If bytesRead = 0 Then

Exit While

End If

requestStream.Write(buffer, 0, bytesRead)

End While

' The request stream must be closed before getting the response.

requestStream.Close()

uploadResponse = uploadRequest.GetResponse()

Console.WriteLine("Upload complete.")

Catch ex As UriFormatException

Console.WriteLine(ex.Message)

Catch ex As IOException

Console.WriteLine(ex.Message)

Catch ex As WebException

Console.WriteLine(ex.Message)

Finally

If uploadResponse IsNot Nothing Then

uploadResponse.Close()

End If

If fileStream IsNot Nothing Then

fileStream.Close()

End If

If requestStream IsNot Nothing Then

requestStream.Close()

End If

End Try

End Sub

Micha Brans

FtpWebRequest only supports FTP protocol and FTP with SSL. It does not support SFTP.

Mariya

Mariya Atanasova [NCL]

You can probably make your own implementation using sockets or search the web for someone who had already done that... It won't be trivial. I know that you didn't want to consider 3rd party software, but it is still an option. There are freeSFTP clients you can use...

http://www.google.com/search?hl=en&q=free+sftp

Mariya

Mariya Atanasova [NCL]
Yes, you are right. Writing own implementation is possible, but far from trivial. SFTP has nothing to do with old FTP, it's rather file transfer protocol based on SSH. Therefore you have to implement SSH core first (or embed Open SSH or something similar ).

For differences between FTP, SFTP and FTPS (FTP over TLS/SSL) see http://www.rebex.net/secure-ftp.net/

For more info about SSH File Transfer Protocol (aka SFTP) see http://en.wikipedia.org/wiki/SSH_file_transfer_protocol

And of course you can use one of SFTP commercial components such as Rebex SFTP for .NET - http://www.rebex.net/sftp.net/

Uploading and download could be as easy as in following code (taken from http://www.rebex.net/sftp.net/tutorial-sftp.aspx#upload-download)

// create client, connect and log in
Sftp client = new Sftp();
client.Connect(hostname);
client.Login(username, password);

// upload the 'index.html' file to the specified directory at the server
client.PutFile(@"c:\data\index.html", "/wwwroot/index.html");

client.Disconnect();

I guess that it would be similar with other commercial sftp components.



Martin Vobr
If you want your connection to be encrypted all you need to do is set the EnableSsl field to true and set ClientCertificates property to certificates you want to use in the SSL negotiation.
Amit Paka - MSFT

Hi Amid,

Thanks for that reply but i di already add the enablessl:

Dim uploadRequest As FtpWebRequest = WebRequest.Create(uploadUrl)

uploadRequest.EnableSsl = True

I've search in the help, but I'm unable to find out what to to with the ClientCertificates property.

Can you please post me some additional info about this?

Again, I need to modify the posted sample to use STP in stead of FTP to send a file to a server.

Thanks,

Micha.

Micha Brans
Did you check if the code works in the first place? Or if not what error is it throwing.
Amit Paka - MSFT

Yes, I did test it, otherwise I would not have replied to you.

The error I get is The remote server returned an error: (431) 431 Local error code 2 was returned from tls_init()

This was with the enablessl option true, but without the certificate part.

Micha.

Micha Brans

FtpWebRequest only supports FTP protocol and FTP with SSL. It does not support SFTP.

Mariya

Mariya Atanasova [NCL]
OK, thanks for that message, any ideas on how to procede....
Micha Brans

You can probably make your own implementation using sockets or search the web for someone who had already done that... It won't be trivial. I know that you didn't want to consider 3rd party software, but it is still an option. There are freeSFTP clients you can use...

http://www.google.com/search?hl=en&q=free+sftp

Mariya

Mariya Atanasova [NCL]
Yes, you are right. Writing own implementation is possible, but far from trivial. SFTP has nothing to do with old FTP, it's rather file transfer protocol based on SSH. Therefore you have to implement SSH core first (or embed Open SSH or something similar ).

For differences between FTP, SFTP and FTPS (FTP over TLS/SSL) see http://www.rebex.net/secure-ftp.net/

For more info about SSH File Transfer Protocol (aka SFTP) see http://en.wikipedia.org/wiki/SSH_file_transfer_protocol

And of course you can use one of SFTP commercial components such as Rebex SFTP for .NET - http://www.rebex.net/sftp.net/

Uploading and download could be as easy as in following code (taken from http://www.rebex.net/sftp.net/tutorial-sftp.aspx#upload-download)

// create client, connect and log in
Sftp client = new Sftp();
client.Connect(hostname);
client.Login(username, password);

// upload the 'index.html' file to the specified directory at the server
client.PutFile(@"c:\data\index.html", "/wwwroot/index.html");

client.Disconnect();

I guess that it would be similar with other commercial sftp components.



Martin Vobr
Hi Mariya,

Is this problem, in any way, related to the issue with regards to using FtpWebRequest to connect with Alpha Systems (e.g. VMS)? I've read from some other thread that FtpWebRequest cannot process ftp commands when connecting to the alpha system. Your reply will be mostly appreciated. Thanks
gwen_newg

Not at all. FtpWebRequest just does not support the SFTP protocol (only FTP and FTP over SSL)

Mariya

Mariya Atanasova [NCL]
SFTP and FTPS (FTP over SSL) are indeed very different and are often confused. The SFTP protocol is actually a subsystem of the SSH protocol whereas the FTPS protocol runs on top of the FTP protocol using an SSL encrypted channel. Unlike the FTPS protocol the SSH/SFTP protocol is a packet based protocol whereas the FTPS protocol is command based. For this reason you will find that FTPS is generally faster than SFTP/SSH. There are also two different types of FTPS, explicit-SSL and implicit-SSL to add to the confusion.

Explicit-SSL - Connection starts on standard FTP port (usually 21) in non-encrypted state. Client then sends AUTH SSL or AUTH TLS command to server requesting that it switches to encrypted channel before sending user credentials. The advantage of explicit-SSL is that the server can handle both encrypted and non-encrypted sessions on the same port (21).

Implicit-SSL - Unlike explicit-SSL, this connection starts off encrypted and there is no going to an unencrypted session. This usually runs on port 990. Advantage is that you can force users to connect using encrypted session, though some servers also allow you to do this at account level in Explicit-SSL.

Some API for SFTP and FTPS are:

FTP/FTPS

http://www.jscape.com/articles/ftp_using_csharp.html

SFTP/SSH

http://www.jscape.com/articles/sftp_using_csharp.html

  • Proposed As Answer bypauts Sunday, October 12, 2008 11:51 AM
  •  
vglass
Also take a look at edtFTPnet/PRO, which is a .NET library supporting SFTP (as well as FTP and FTPS). Loads of features, GUI components, and a single interface enabling easy swapping between protocols.
bblackshaw
Pls have a look at SharpSSH.

Its Open Source. I have been using this. Its nice.

http://www.tamirgal.com/blog/page/SharpSSH.aspx

Regards
Jegan
  • Proposed As Answer byjeganinfo Tuesday, September 15, 2009 2:50 PM
  •  
jeganinfo

You can use google to search for other answers

Custom Search

More Threads

• Help: Get Directory Info from Remote machine
• Caller ID on an incoming Call via Modem
• Find the registered C# Dll in Clinet's Machine and invoke its methods.
• transactions in msmq 3.0
• Socket SendFile prb
• Inter windows form communication
• WCF endpoint connection issue in windows service
• Sending mails on a localhost IIS SMTP Server
• grid scheduler based on web service
• Secure Socket