C#获取IP地址
2014-01-01 10:09
197 查看
1、获取客户端IP
2、服务器端获取客户端请求IP和客户端机器名称
/// <summary> /// 获取客户端Ip /// </summary> /// <returns></returns> public String GetClientIp() { String clientIP = ""; if (System.Web.HttpContext.Current != null) { clientIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(clientIP) || (clientIP.ToLower() == "unknown")) { clientIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"]; if (string.IsNullOrEmpty(clientIP)) { clientIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } } else { clientIP = clientIP.Split(',')[0]; } } return clientIP; }
2、服务器端获取客户端请求IP和客户端机器名称
/// <summary> /// 服务器端获取客户端请求IP和客户端机器名称 /// </summary> public static void GetClientInfo() { OperationContext context = OperationContext.Current; MessageProperties messageProperties = context.IncomingMessageProperties; RemoteEndpointMessageProperty endpointProperty = messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty; HttpRequestMessageProperty requestProperty = messageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty; string clientIp = !string.IsNullOrEmpty(requestProperty.Headers["X-Real-IP"]) ? requestProperty.Headers["X-Real-IP"] : endpointProperty.Address; string clientName = Environment.MachineName; Console.WriteLine("ClientIp: " + clientIp + "clientName:" + clientName); }
相关文章推荐
- C#获取网页的HTML码、下载网站图片、获取IP地址
- C#/.NET获取本机的IP地址
- C# 网络编程之获取本机名、ip地址、域名、物理位置
- C# NTP 时钟同步,获取指定IP地址的系统时间及错误问题解决。
- C# 获取本机ip地址
- [置顶] C#中通过调用webService获取上网IP地址的区域的方法
- c#中获取cpu序列号、网卡mac地址、ip地址、硬盘序列号、登录用户名、pc类型、计算机名称、物理内存
- C#获取本机IP地址和Mac地址的方法
- C#中获取CPU序列号、网卡Mac地址、IP地址、硬盘序列号、登录用户名、PC类型、计算机名称、物理内存
- C# 网站 获取客户端IP地址详细信息
- C#获取本机IP地址
- C# 获取本地IP地址
- c#获取IP地址及端口号
- C#中ip地址的获取 .
- C# 根据IP地址获取城市
- .net C# 获取硬盘号 CPU序列号 IP地址 Mac地址
- C# 获取本地IP地址以及MAC地址
- C#中通过调用webService获取上网IP地址的区域的方法
- (转)【ASP.NET开发】获取客户端IP地址 via C#
- C#获取真实IP地址实现方法