使用Asp.Net C#检查用户的互联网连接

问题描述:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Internet_Connectivity.aspx.cs" Inherits="Internet_Connectivity" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head  runat="server">
    <title>How to check user internet connection</title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
    <asp:Label runat="server" ID="lblCaption" Font-Bold="true" ForeColor="DarkBlue">To test your connection click below
    

    

    <asp:Button runat="server" ID="cmdCHeck" Text="Check Connectivity" OnClick="cmdCHeck_Click" />
    </div>
    </form>
</body>
</html>





using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;

public partial class Internet_Connectivity : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void cmdCHeck_Click(object sender, EventArgs e)
    {
        WebClient workstation = new WebClient();
        byte[] data = null;
        try
        {
            data = workstation.DownloadData("http://www.google.com");
        }
        catch (Exception ex)
        {
        }
        if (data != null && data.Length > 0) 
            lblCaption.Text="You are connected.";
        else
            lblCaption.Text="You are not connected.";
    }
}

您希望它如何工作?
如果我需要去一个网站检查我的互联网连接是否正常,为什么我必须按下一个按钮才能找到答案?
如果可以看到该死的按钮,则表明我已经连接到互联网,并且可以正常使用...
How do you expect that to work?
If I need to go to a website to check my internet connection is working, why would I have to press a button to find out?
If I can see the damn button, I have connected to the internet and it is working...