Test FTP Connection?

newguy

Well-known member
Joined
Jun 30, 2008
Messages
611
Location
Denver Co, USA
Programming Experience
1-3
I have an FTP class for my app, in setting up the hostname, username and password I was wanting to add a <test connection> function. What would a good approach be for this? This is for the settings page. I just want to test the info they enter without uploading/downloading.

Thanks...
 
Thanks for the responses. JMC I don't understand how to get the data back to compare it, from what I read it gets the current directory. Thanks Bobby, but I really don't know what you mean??? Any chance you could elaborate? This is what I have so far.
VB.NET:
Try
   Dim request As FtpWebRequest = DirectCast(FtpWebRequest.Create(Host), FtpWebRequest)
   request.KeepAlive = False
   request.Credentials = New NetworkCredential(username, password)
   request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory

Catch ex As Exception
   MessageBox.Show("Error : " & ex.Message)
End Try
:confused:
 
Last edited:
Hold on I think I got it. I need to make a stream to read the response. First thing I need to do is create a directory, cause right now it is blank - so it's returning blank.
 
Ok, I got the new directory, but still shooting blanks (so to speak). 's' returns an empty string and not the directory name. :confused:
VB.NET:
Try
   Dim request As FtpWebRequest = DirectCast(FtpWebRequest.Create(Host), FtpWebRequest)
   request.KeepAlive = False
   request.Credentials = New NetworkCredential(username, password)
   request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory
   Dim response As WebResponse = request.GetResponse
   Using stream As Stream = response.GetResponseStream
      Using readStream As New StreamReader(stream)
          Dim s As String = readStream.ReadToEnd()
          MessageBox.Show(s)
      End Using
   End Using
Catch ex As Exception
    MessageBox.Show("Error : " & ex.Message)
End Try
What am I doing wrong?
 
Last edited:
You can also check the status code of response:
VB.NET:
Dim response As Net.FtpWebResponse = CType(request.GetResponse, Net.FtpWebResponse)
If response.StatusCode = Net.FtpStatusCode.PathnameCreated Then
 
It works great, and it catches all the possible mistakes that could be made with the diff fields - which is what I require, so thanks a bunch.
 
Back
Top