Thursday, November 10, 2011

Check user internet connectivity using Asp.Net C#

In some cases specially for AJAX projects Asp.net C# VB.Net developers need to check user internet connection before attempting to connect to remote server. Also another important usage is for dashboard where after certain time interval you may need to know does the user connected with the network or not. Here in this article i will describe how one can find user internet connectivity using asp.net C# server side code.

The strategy is to start download a page from server. If you can download means you are connected to the internet otherwise not. Here i am using Google page but you can use any simple page for better performance. Look at the below code:






01WebClient workstation = new WebClient();
02        byte[] data = null;
03 
04        try
05        {
06            data = workstation.DownloadData("http://www.google.com");
07        }
08        catch (Exception ex)
09        {
10        }
11         
12        if (data != null && data.Length > 0)
13            lblCaption.Text="You are connected.";
14        else
15            lblCaption.Text="You are not connected.";
To complete an example first add a page in your project. Then write the below Markup Code:
01<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Internet_Connectivity.aspx.cs" Inherits="Internet_Connectivity" %>
02 
03<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 
05<html xmlns="http://www.w3.org/1999/xhtml" >
06<head runat="server">
07    <title>How to check user internet connection</title>
08</head>
09<body>
10    <form id="form1" runat="server">
11    <div>
12    <asp:Label runat="server" ID="lblCaption" Font-Bold="true" ForeColor="DarkBlue">To test your connection click below</asp:Label>
13     
14 
15     
16 
17    <asp:Button runat="server" ID="cmdCHeck" Text="Check Connectivity" OnClick="cmdCHeck_Click" />
18    </div>
19    </form>
20</body>
21</html>
Under code behind the complete code is given below:
01using System;
02using System.Data;
03using System.Configuration;
04using System.Collections;
05using System.Web;
06using System.Web.Security;
07using System.Web.UI;
08using System.Web.UI.WebControls;
09using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Net;
12 
13public partial class Internet_Connectivity : System.Web.UI.Page
14{
15    protected void Page_Load(object sender, EventArgs e)
16    {
17 
18    }
19    protected void cmdCHeck_Click(object sender, EventArgs e)
20    {
21        WebClient workstation = new WebClient();
22        byte[] data = null;
23        try
24        {
25            data = workstation.DownloadData("http://www.google.com");
26        }
27        catch (Exception ex)
28        {
29        }
30        if (data != null && data.Length > 0)
31            lblCaption.Text="You are connected.";
32        else
33            lblCaption.Text="You are not connected.";
34    }
35}
Don't forget to add System.Net namespace into your page.

The output will be:
Check Internet Connection

No comments:

Post a Comment