在asp.net开发中经常有不少人问过如何获取客户端和服务器的有关信息的问题,现在我做了一个简单页面,利用了一个简单办法,就可以获取很多有用的信息,如当前文件的物理目录和路径、服务器使用的端口、服务器版本、用户浏览器版本等。现在将代码提供给大家,以便参考。
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<%@ Import Namespace="System.Collections.Specialized" %>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>获取服务器及客户端有关属性</title>
</head>
<body>
<script runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
if (!IsPostBack)
{
NameValueCollection nvc=Request.ServerVariables;
for (int i = 0; i < nvc.Count; i++)
{
Response.Write(nvc.GetKey(i) + "=" + nvc.Get(nvc.GetKey(i))+"<br>");
}
}
}
</script>
</body>
</html>
<%@ Import Namespace="System.Collections.Specialized" %>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>获取服务器及客户端有关属性</title>
</head>
<body>
<script runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
if (!IsPostBack)
{
NameValueCollection nvc=Request.ServerVariables;
for (int i = 0; i < nvc.Count; i++)
{
Response.Write(nvc.GetKey(i) + "=" + nvc.Get(nvc.GetKey(i))+"<br>");
}
}
}
</script>
</body>
</html>
说明:以上是获取了所有的信息,如果只想获取某个指定信息,可以用nvc.Get(string keyName)的办法,例如nvc.Get("SERVER_SOFTWARE")就可以获取服务器版本信息。
一下是我在本机运行时的运行信息:
ALL_HTTP=HTTP_CONNECTION:Keep-Alive HTTP_ACCEPT:*/* HTTP_ACCEPT_ENCODING:gzip, deflate HTTP_ACCEPT_LANGUAGE:zh-cn HTTP_HOST:localhost HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; InfoPath.2; .NET CLR 2.0.50727) HTTP_UA_CPU:x86
ALL_RAW=Connection: Keep-Alive Accept: */* Accept-Encoding: gzip, deflate Accept-Language: zh-cn Host: localhost User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; InfoPath.2; .NET CLR 2.0.50727) UA-CPU: x86
APPL_MD_PATH=/LM/W3SVC/1/ROOT
APPL_PHYSICAL_PATH=c:\inetpub\wwwroot\
AUTH_TYPE=
AUTH_USER=
AUTH_PASSWORD=
LOGON_USER=
REMOTE_USER=
CERT_COOKIE=
ALL_HTTP=HTTP_CONNECTION:Keep-Alive HTTP_ACCEPT:*/* HTTP_ACCEPT_ENCODING:gzip, deflate HTTP_ACCEPT_LANGUAGE:zh-cn HTTP_HOST:localhost HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; InfoPath.2; .NET CLR 2.0.50727) HTTP_UA_CPU:x86
ALL_RAW=Connection: Keep-Alive Accept: */* Accept-Encoding: gzip, deflate Accept-Language: zh-cn Host: localhost User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; InfoPath.2; .NET CLR 2.0.50727) UA-CPU: x86
APPL_MD_PATH=/LM/W3SVC/1/ROOT
APPL_PHYSICAL_PATH=c:\inetpub\wwwroot\
AUTH_TYPE=
AUTH_USER=
AUTH_PASSWORD=
LOGON_USER=
REMOTE_USER=
CERT_COOKIE=
本文转自周金桥51CTO博客,原文链接: http://blog.51cto.com/zhoufoxcn/166979,如需转载请自行联系原作者