SFTP using FtpWebRequest class?
-
I am attempting to read an SFTP directory using the FtpWebRequest class, but am getting the error 'Unable to connect to the remote server'. Here is my code:
Private Sub List(ByVal listUrl As String, ByVal username As String, ByVal password As String) Dim reader As StreamReader = Nothing Dim listRequest As FtpWebRequest = WebRequest.Create(New System.Uri(listUrl)) System.Net.ServicePointManager.CertificatePolicy = New TrustAllCertificatePolicy listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails listRequest.KeepAlive = True listRequest.EnableSsl = True listRequest.Credentials = New NetworkCredential(username, password) Dim listResponse As FtpWebResponse = listRequest.GetResponse() reader = New StreamReader(listResponse.GetResponseStream()) End Sub Public Class TrustAllCertificatePolicy Implements System.Net.ICertificatePolicy Public Function CheckValidationResult(ByVal srvPoint As System.Net.ServicePoint, _ ByVal certificate As System.Security.Cryptography.X509Certificates.X509Certificate, _ ByVal request As System.Net.WebRequest, ByVal certificateProblem As Integer) As Boolean Implements System.Net.ICertificatePolicy.CheckValidationResult Return True End Function End Class
I appreciate any advice you can provide into what I'm missing. Thank you, -
I am attempting to read an SFTP directory using the FtpWebRequest class, but am getting the error 'Unable to connect to the remote server'. Here is my code:
Private Sub List(ByVal listUrl As String, ByVal username As String, ByVal password As String) Dim reader As StreamReader = Nothing Dim listRequest As FtpWebRequest = WebRequest.Create(New System.Uri(listUrl)) System.Net.ServicePointManager.CertificatePolicy = New TrustAllCertificatePolicy listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails listRequest.KeepAlive = True listRequest.EnableSsl = True listRequest.Credentials = New NetworkCredential(username, password) Dim listResponse As FtpWebResponse = listRequest.GetResponse() reader = New StreamReader(listResponse.GetResponseStream()) End Sub Public Class TrustAllCertificatePolicy Implements System.Net.ICertificatePolicy Public Function CheckValidationResult(ByVal srvPoint As System.Net.ServicePoint, _ ByVal certificate As System.Security.Cryptography.X509Certificates.X509Certificate, _ ByVal request As System.Net.WebRequest, ByVal certificateProblem As Integer) As Boolean Implements System.Net.ICertificatePolicy.CheckValidationResult Return True End Function End Class
I appreciate any advice you can provide into what I'm missing. Thank you, -
-
Here is the C# version - I'm really not interested in getting into a C# is better then vb debate. I just want get this working so I can move on to the next item on my to do list.
private void List(string listUrl, string username, string password) { StreamReader reader = null; FtpWebRequest listRequest = WebRequest.Create(new System.Uri(listUrl)); System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy(); listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; listRequest.KeepAlive = true; listRequest.EnableSsl = true; listRequest.Credentials = new NetworkCredential(username, password); FtpWebResponse listResponse = listRequest.GetResponse(); reader = new StreamReader(listResponse.GetResponseStream()); } public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy { public bool CheckValidationResult(System.Net.ServicePoint srvPoint, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Net.WebRequest request, int certificateProblem) { return true; } }
-
I am attempting to read an SFTP directory using the FtpWebRequest class, but am getting the error 'Unable to connect to the remote server'. Here is my code:
Private Sub List(ByVal listUrl As String, ByVal username As String, ByVal password As String) Dim reader As StreamReader = Nothing Dim listRequest As FtpWebRequest = WebRequest.Create(New System.Uri(listUrl)) System.Net.ServicePointManager.CertificatePolicy = New TrustAllCertificatePolicy listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails listRequest.KeepAlive = True listRequest.EnableSsl = True listRequest.Credentials = New NetworkCredential(username, password) Dim listResponse As FtpWebResponse = listRequest.GetResponse() reader = New StreamReader(listResponse.GetResponseStream()) End Sub Public Class TrustAllCertificatePolicy Implements System.Net.ICertificatePolicy Public Function CheckValidationResult(ByVal srvPoint As System.Net.ServicePoint, _ ByVal certificate As System.Security.Cryptography.X509Certificates.X509Certificate, _ ByVal request As System.Net.WebRequest, ByVal certificateProblem As Integer) As Boolean Implements System.Net.ICertificatePolicy.CheckValidationResult Return True End Function End Class
I appreciate any advice you can provide into what I'm missing. Thank you, -
Jason Weibel wrote:
I am attempting to read an SFTP directory using the FtpWebRequest class
Are you sure the Server is running?
led mike
The server is running and I have no problem connecting with WS-FTP. I'm starting to wonder if FtpWebRequest works for SFTP. I have been reading some blogs and others are having the same problem but no one has a solution. Here are some links: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=346870&SiteID=1 http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=532122&SiteID=1 At this point I'm going to look into other ways to get this resolved.
-
The server is running and I have no problem connecting with WS-FTP. I'm starting to wonder if FtpWebRequest works for SFTP. I have been reading some blogs and others are having the same problem but no one has a solution. Here are some links: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=346870&SiteID=1 http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=532122&SiteID=1 At this point I'm going to look into other ways to get this resolved.