.NET中获取IP地址
1. 在ASP.NET中专用属性:
获取服务器电脑名:Page.Server.ManchineName
获取用户信息:Page.User
获取客户端电脑名:Page.Request.UserHostName
获取客户端电脑IP:Page.Request.UserHostAddress
2. 在网络编程中的通用方法:
获取当前电脑名:static System.Net.Dns.GetHostName()
根据电脑名取出全部IP地址:static System.Net.Dns.Resolve(电脑名).AddressList
也可根据IP地址取出电脑名:static System.Net.Dns.Resolve(IP地址).HostName
3. 系统环境类的通用属性:
当前电脑名:static System.Environment.MachineName
当前电脑所属网域:static System.Environment.UserDomainName
当前电脑用户:static System.Environment.UserName
Request.UserHostAddress
string IP = (Request.ServerVariables["HTTP_VIA"]!=null)?Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString():Request.ServerVariables["REMOTE_ADDR"].ToString();
string ipAddress = System.Web.HttpContext.Current.Request.UserHostAddress;
先引用命名空间:using System.Net;
得到客户端IP地址:Request.ServerVariables["REMOTE_ADDR"]或Rqurest.UserHostAddress();
得到本机IP地址:Request.ServerVariables["local_addr"]或addr = new System.Net.IPAddress ( Dns.GetHostByName ( Dns.GetHostName ( ) ) .AddressList [0].Address ) ;
HttpBrowserCapabilities bc= Request.Browser;
Label3.Text=bc.Browser+" "+bc.Version;//得到浏览器版本
Label2.Text=bc.Platform;//得到操作系统
hostname=Dns.GetHostName();
IPHostEntry jj=Dns.GetHostByName(hostname);//得到IP
//IPAddress ip=Dns.Resolve(hostname);
Label1.Text=jj.AddressList[0].ToString();