博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

ASP.NET获取客户端相关信息

Posted on 2007-11-28 12:11  迷梦江南  阅读(1692)  评论(0编辑  收藏  举报

ASP.NET获取客户端信息(IP,浏览器,操作系统等信息)在网上能够找到很多例子,我在这里只是把他们收集起来,以便以后查看。
我做了下面这个示例:
HTML:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Demo_Default" %>

<!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>无标题页</title>
    <script type="text/javascript" src="../mootools-release-1.11.js"></script>
    <script type="text/javascript" >
        window.addEvent('domready',function(){
           
            $('getInfo').addEvent('click',function(){
                url="Default.aspx";
                var width=window.screen.width;
                var height=window.screen.height;
                var postData="getInfo=true&width="+width+"&height="+height;
                new Ajax(url,{method:'post',onComplete:function(){
                        $('ipdiv').innerHTML=this.response.text;
                    }
                }).request(postData);
            });
        });
    </script>
</head>
<body>
<div id="ipdiv"></div><br/><input type="button" id="getInfo" name="getInfo" value="getInfo" />
</body>
</html>


CS:

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;

public partial class Demo_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(Request["getInfo"]))
        {
            string info ="客户端IP:"+ Page.Request.UserHostAddress;
            info += "<br/>用户信息:" + Page.User;
            info += "<br/>服务器电脑名称:" + Page.Server.MachineName;
            info += "<br/>当前用户电脑名称:" + System.Net.Dns.GetHostName();
            info += "<br/>当前电脑名:" + System.Environment.MachineName;
            info += "<br/>当前电脑所属网域:" + System.Environment.UserDomainName;
            info += "<br/>当前电脑用户:" + System.Environment.UserName;
            info += "<hr/>";
            info += "<br/>浏览器类型:"+Request.Browser.Browser;
            info += "<br/>浏览器标识:" + Request.Browser.Id;
            info += "<br/>浏览器版本号:" + Request.Browser.Version;
            info += "<br/>浏览器是不是测试版本:"+Request.Browser.Beta;
            info += "<br/>浏览器的分辨率(像素):" + Request["width"].ToString() + "*" + Request["height"].ToString();//1280/1024
            info += "<hr/>";
            info += "<br/>客户端的操作系统:"+Request.Browser.Platform;
            info += "<br/>是不是win16系统:"+Request.Browser.Win16;
            info += "<br/>是不是win32系统:" +Request.Browser.Win32;
            Response.Write(info);
            Response.End();
        }
    }
}