VB.NET WinForms:
How to Check Internet Connectivity Best Methods & Code Examples
Introduction
Ensuring an active internet connection is essential for many applications—especially those that rely on APIs, cloud services, or remote databases. This guide will show different methods to check internet connectivity in a VB.NET WinForms application.
1. Using Ping to Check Connectivity
Any Desktop application developer knows that simplest way to verify an internet connection is bypinging a reliable server likeGoogle.
Imports System.Net.NetworkInformation
Function CheckInternetPing() As Boolean
Try
Dim ping As New Ping()
Dim reply As PingReply = ping.Send("8.8.8.8") ' Google DNS
Return (reply.Status = IPStatus.Success)
Catch
Return False
End Try
End Function
' Usage:
If CheckInternetPing() Then
MessageBox.Show("Internet is connected!")
Else
MessageBox.Show("No internet connection.")
End If
2. Checking Connectivity via HTTP Request
Instead of using Ping, an alternative approach is to send a web request to a known website.
Imports System.Net
Function CheckInternetHttp() As Boolean
Try
Dim request As HttpWebRequest = CType(WebRequest.Create("https://www.google.com"), HttpWebRequest)
request.Timeout = 5000
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
Return (response.StatusCode = HttpStatusCode.OK)
Catch
Return False
End Try
End Function
' Usage:
If CheckInternetHttp() Then
MessageBox.Show("Internet is available!")
Else
MessageBox.Show("No internet connection.")
End If
3. Using System APIs for Network Availability
Developers know that MS-WINDOWS system provides built-in network availability checks through the NetworkInterface class.
Imports System.Net.NetworkInformation
Function CheckInternetNetworkInterface() As Boolean
Return NetworkInterface.GetIsNetworkAvailable()
End Function
' Usage:
If CheckInternetNetworkInterface() Then
MessageBox.Show("Internet is active!")
Else
MessageBox.Show("No network connection.")
End If
4. Best Practices for Internet Connection Checks
- Use multiple methods (Ping + WebRequest) to ensure reliable detection.
- Handle timeouts properly to prevent delays in application responsiveness.
- Consider event-based network detection for real-time monitoring.
- Ensure proper exception handling to prevent application crashes.
Conclusion
By using Ping, HTTP Requests, and System APIs, developers can reliably check internet connectivity in VB.NET WinForms applications.Each method serves different purposes depending on the application's requirements.
VB.NET WinForms: Legacy but effective way to
Check Internet Connection [2]
This is a very simple example of how to check if there is an internet connection available for your machine (LapTop, PC) using VB 2010 Programming Language.
- Project Design :
VB.NET Check Internet Connection |
- Coding
In the following VB.NET Code I'm using two Functions to (Boolean) :
- Check internet connection using Computer.Network.Ping("google.com")
- Check internet connection Using stream = client.OpenRead("https://www.google.com")
Imports System.Net
Imports System.Net.Http
Public Class Form1
Function IsConnected() As Boolean
'Checks the internet connection
If My.Computer.Network.Ping("google.com").ToString = True Then
Label1.Text = ("Connected")
Label1.ForeColor = Color.Green
Else
Label1.Text = ("No Connection")
Label1.ForeColor = Color.Red
End If
Return True
End Function
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Timer1.Tick
'Check if there is an internet connection is available.
IsConnected()
End Sub
'Another function to check for internet connectivity
Private Async Function CheckInternetConnectionAsync() As Task(Of Boolean)
Try
Using client As New HttpClient()
' Set a short timeout
client.Timeout = TimeSpan.FromSeconds(3)
' Send a HEAD request (faster than GET)
Dim request = New HttpRequestMessage(HttpMethod.Head, "https://www.google.com")
Dim response = Await client.SendAsync(request)
Return response.IsSuccessStatusCode
End Using
Catch
Return False
End Try
End Function
'💡 Why Use HttpClient + HttpMethod.Head
'HttpClient Is the modern, thread-safe class optimized for HTTP use.
'HEAD Is faster than GET And uses less data—ideal for connectivity checks.
'You can reuse HttpClient If calling frequently (but here it Is fine To dispose after use).
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim isOnline = Await CheckInternetConnectionAsync()
MessageBox.Show("Internet Available: " & isOnline.ToString())
End Sub
End Class
♥ Here are some online Visual Basic lessons and courses: