Hi friends, in
this article I will explain about Check Internet Connection availability in
ASP. Net, Check Internet Connection in
Asp.net using VB.NET /C#, how to check the internet connectivity using ASP.NET
or how to Test Internet Connectivity.
In previous articles i explained about How to change the default browser in Visual Studio, Setting the default browser inside Visual Studio, Default Browser Switcher in Visual Studio,Fragment Caching in ASP.NET or How To Perform Fragment Caching in ASP.NET by Using VB.NET/C# or Fragment Caching Example, Page Fragment Caching in ASP.NET
I will explain
about Check Internet Connection availability in ASP.NET by downloading my
website from the server. Below is the code to Check Internet Connection
availability in ASP.NET using VB.NET or C#.
In ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml"
>
<head runat="server">
<title>Aspdotnet-Roja:Check Internet Connection
Availability using ASP.NET</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:Button ID="Button1"
runat="server"
Text="Check
Internet Connectivity" Height="32px" Width="264px" />
<asp:Label ID="netconlbl"
runat="server"
Text="Label"
></asp:Label>
</div>
</form>
</body>
</html>
|
Then Import
Below namespaces.
Imports System
Imports System.Net
|
In VB.NET:
Imports System
Imports System.Net
Partial Class netcon
Inherits System.Web.UI.Page
Protected Sub
Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
netconlbl.Visible = False
End Sub
Protected Sub
Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim client As
New WebClient()
Dim datasize As
Byte() = Nothing
Try
datasize = client.DownloadData("http://aspdotnet-roja.blogspot.com")
Catch ex As
Exception
End Try
netconlbl.Visible = True
If datasize IsNot
Nothing AndAlso
datasize.Length > 0 Then
netconlbl.Text = "Internet Connection
Available."
Else
netconlbl.Text = "Internet Connection
Not Available."
End If
End Sub
End Class
|
In C#:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Net;
partial class netcon
: System.Web.UI.Page
{
protected void Page_Load(object
sender, System.EventArgs e)
{
netconlbl.Visible
= false;
}
protected void Button1_Click(object sender, System.EventArgs e)
{
WebClient
client = new WebClient();
byte[] datasize = null;
try {
datasize
= client.DownloadData("http://aspdotnet-roja.blogspot.com");
}
catch (Exception ex) {
}
netconlbl.Visible
= true;
if (datasize != null && datasize.Length
> 0) {
netconlbl.Text
= "Internet Connection Available.";
}
else {
netconlbl.Text
= "Internet Connection Not Available.";
}
}
Public netcon()
{
Load
+= Page_Load;
}
}
|
Run the above page.
The output of above page is as shown in the below figure.
Thanks for reading my Aspdotnet-Roja blog, please give me your comment or feedback. Keep reading and sharing your knowledge!
No comments:
Post a Comment