您的位置:首页 > 编程语言 > ASP

asp.net获取服务器信息

2016-04-03 22:25 633 查看


asp.net获取服务器信息

1.获取IP地址

服务端获取
//方法一
HttpContext.Current.Request.UserHostAddress;

//方法二
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

//方法三
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

//方法四(无视代理)
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

//方法五
if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // using proxy
{
ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();  //
4000
Return real client IP.
}
else// not using proxy or can't get the Client IP
{
ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
}

客户端获取
//方法六
var ip = '<!--#echo var="REMOTE_ADDR"-->';
alert("Your IP address is "+ip);

//方法七(无视代理)
function GetLocalIPAddress()
{
var obj = null;
var rslt = "";
try
{
obj = new ActiveXObject("rcbdyctl.Setting");
rslt = obj.GetIPAddress;
obj = null;
}
catch(e)
{
//
}

return rslt;
}


 
2.获取当前页面地址信息

设当前页完整地址是:http://www.jb51.net/aaa/bbb.aspx?id=5&name=kelli

"http://"是协议名

"www.jb51.net"是域名

"aaa"是站点名

"bbb.aspx"是页面名(文件名)

"id=5&name=kelli"是参数

【1】获取 完整url (协议名+域名+站点名+文件名+参数)

代码如下:

string url=Request.Url.ToString();

url= http://www.jb51.net/aaa/bbb.aspx?id=5&name=kelli
【2】获取 站点名+页面名+参数:

代码如下:

string url=Request.RawUrl;

(或 string url=Request.Url.PathAndQuery;)

url= /aaa/bbb.aspx?id=5&name=kelli

【3】获取 站点名+页面名:

 代码如下:

string url=HttpContext.Current.Request.Url.AbsolutePath;

(或 string url= HttpContext.Current.Request.Path;)

url= aaa/bbb.aspx

【4】获取 域名:

代码如下:

string url=HttpContext.Current.Request.Url.Host;

url= www.jb51.net

【5】获取 参数:

代码如下:

string url= HttpContext.Current.Request.Url.Query;

url= ?id=5&name=kelli

 代码如下:

Request.RawUrl:获取客户端请求的URL信息(不包括主机和端口)------>/Default2.aspx

Request.ApplicationPath:获取服务器上ASP.NET应用程序的虚拟路径。------>/

Request.CurrentExecutionFilePath:获取当前请求的虚拟路径。------>/Default2.aspx

Request.Path:获取当前请求的虚拟路径。------>/Default2.aspx

Request.PathInfo:取具有URL扩展名的资源的附加路径信息------>

Request.PhysicalPath:获取与请求的URL相对应的物理文件系统路径。------>E:\temp\Default2.aspx

Request.Url.LocalPath:------>/Default2.aspx

Request.Url.AbsoluteUri:------>http://localhost:8080/Default2.aspx

Request.Url.AbsolutePath:---------------------------->/Default2.aspx
 
3.获取服务器信息

protected void Page_Load(object sender, EventArgs e)
{
//获取服务器名
this.serverName.Text = "http://" + Request.Url.Host;
//获取服务器IP
this.serverIP.Text = Request.ServerVariables.Get("Local_Addr").ToString();
//获取服务器操作系统版本
this.serverSystem.Text = GetSystem();
//获取管理系统当前目录
this.serverPath.Text = Request.PhysicalApplicationPath;
//获取服务器IIS版本
this.serverIIS.Text = Request.ServerVariables["SERVER_SOFTWARE"].ToString();
//获取服务器当前时间
this.serverDate.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
//检测IE版本
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Version Vector");
this.serverIE.Text = key.GetValue("IE", "未检测到").ToString();
//检测访问端口
this.serverPort.Text = Request.ServerVariables.Get("Server_Port").ToString();
}

/// <summary>
/// 获取操作系统版本
/// </summary>
/// <returns></returns>
ddd0
;
private string GetSystem()
{
string system = Request.ServerVariables.Get("HTTP_USER_AGENT").ToString();
string tmpSys = string.Empty;
if (system.IndexOf("NT 4.0")>0)
{
tmpSys = "Windows NT 4.0";
}
else if (system.IndexOf("NT 5.0")>0)
{
tmpSys = "Windows NT 5.0";
}
else if (system.IndexOf("NT 6.1")>0)
{
tmpSys = "Windows NT 6.1";
}
else
{
tmpSys = "未检测到操作系统信息!";
}
return tmpSys;
}


3.获取服务器信息

Label1.Text = "服务器名称:"+Server.MachineName;//服务器名称
Label2.Text = "服务器IP地址:" + Request.ServerVariables["LOCAL_ADDR"];//服务器IP地址
Label3.Text = "服务器域名:" + Request.ServerVariables["SERVER_NAME"];//服务器域名
Label4.Text = ".NET解释引擎版本:" + ".NET CLR" + Environment.Version.Major + "." + Environment.Version.Minor + "." + Environment.Version.Build + "." + Environment.Version.Revision;//.NET解释引擎版本
Label5.Text = "服务器操作系统版本:" + Environment.OSVersion.ToString();//服务器操作系统版本
Label6.Text = "服务器IIS版本:" + Request.ServerVariables["SERVER_SOFTWARE"];//服务器IIS版本
Label7.Text = "HTTP访问端口:" + Request.ServerVariables["SERVER_PORT"];//HTTP访问端口
Label8.Text = "虚拟目录的绝对路径:" + Request.ServerVariables["APPL_RHYSICAL_PATH"];//虚拟目录的绝对路径
Label9.Text = "执行文件的绝对路径:" + Request.ServerVariables["PATH_TRANSLATED"];//执行文件的绝对路径
Label10.Text = "虚拟目录Session总数:" + Session.Contents.Count.ToString();//虚拟目录Session总数
Label11.Text = "虚拟目录Application总数:" + Application.Contents.Count.ToString();//虚拟目录Application总数
Label12.Text = "域名主机:" + Request.ServerVariables["HTTP_HOST"];//域名主机
Label13.Text = "服务器区域语言:" + Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"];//服务器区域语言
Label14.Text = "用户信息:" + Request.ServerVariables["HTTP_USER_AGENT"];
Label14.Text="CPU个数:"+Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS");//CPU个数
Label15.Text = "CPU类型:" + Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER");//CPU类型
Label16.Text = "进程开始时间:" + GetPrStart();//进程开始时间
Label17.Text = "AspNet 内存占用:" + GetAspNetN();//AspNet 内存占用
Label18.Text = "AspNet CPU时间:" + GetAspNetCpu();//AspNet CPU时间
Label19.Text = "FSO 文本文件读写:" + Check("Scripting.FileSystemObject");//FSO 文本文件读写
Label20.Text = "应用程序占用内存" + GetServerAppN();//应用程序占用内存


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: