Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 81
  • Last Modified:

Get Connection Status

How do you check connection status using .Net Framework 4.5?

Here's what I have:

Imports System.Net

Public Class FTPPlus2
   Shared Property HostName As String
   Shared Property Password As String
   Shared Property UserID As String
   Shared Property Target As String
   Shared Property ID As String
   Shared Property SiteName As String

   Shared Property request As FtpWebRequest
   Shared Property response As FtpWebResponse

   Shared Sub Disconnect()
      If response IsNot Nothing Then
         response.Close()
      End If
   End Sub

   Shared Sub Connect()
      request = DirectCast(WebRequest.Create(String.Format("ftp://{0}", HostName)), FtpWebRequest)
   End Sub

   Shared Sub ChangeCurrentFolder(FolderName)

   End Sub

   Shared Function Connected As Boolean
      Dim ret As Boolean  = False

      Return ret
   End Function

   Shared Sub Login()
      request.Credentials = New NetworkCredential(UserID, Password)
   End Sub
End Class

Open in new window

0
Ray Turner
Asked:
Ray Turner
1 Solution
 
Mike EghtebasDatabase and Application DeveloperCommented:
If con.State = con.Open Then
	Connected  = true
Else
	Connected  = false
End If

Open in new window

0
 
cstaff16Commented:
Are you trying to determine if you can connect to the internet?
If so, you can do something like this.  I've used this in various apps and it works well.
You can use any url you want, but I just use google.

using the System.Net.WebClient

public static bool CheckForInternetConnection()
{
    try
    {
        using (var client = new WebClient())
        using (var stream = client.OpenRead("http://www.google.com"))
        {
            return true;
        }
    }
    catch
    {
        return false;
    }
}
0
 
cstaff16Commented:
Sorry, here is vb.net version :).

Public Shared Function CheckForInternetConnection() As Boolean
      Try
            Using client = New WebClient()
                  Using stream = client.OpenRead("http://www.google.com")
                        Return True
                  End Using
            End Using
      Catch
            Return False
      End Try
End Function
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
Ray TurnerSenior ConsultantAuthor Commented:
I'm trying to determine the status of the FtpWebRequest.  Is there a way to determine if the connection is open/successful?
0
 
käµfm³d 👽Commented:
Catch the WebExceptions that GetRequestStream and GetResponse could throw.
0
 
Ray TurnerSenior ConsultantAuthor Commented:
Thanks!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now