在DLL中获取客户端的IP和URL
2010-10-26 08:05:37 来源:WEB开发网核心提示:以下是可在DLL层调用并获取客户端发起的URL、IP、MAC等方法using System;using System.Collections.Generic;using System.Net;using System.Web;namespace FreeShadow{ /// <summary>
以下是可在DLL层调用并获取客户端发起的URL、IP、MAC等方法
using System;using System.Collections.Generic;using System.Net;using System.Web;namespace FreeShadow{ /// <summary> /// 创建人:陈泽湖 /// 创建日期:2010-04-14 /// 功能说明:在DLL里面获取IP和URL /// </summary> public class IPAndUrlHelper { /// <summary> /// 获取主机IP /// </summary> /// <returns></returns> public static string GetHostIP() { string hostIp = string.Empty; try { System.Net.IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList; foreach (IPAddress ip in addressList) { if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { hostIp = ip.ToString(); break; } } return hostIp; } catch (Exception) { return hostIp; } } /// <summary> /// 获取客户端IP /// </summary> /// <returns></returns> public static string GetClientIP() { try { string result = string.Empty; result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(result)) { result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } if (string.IsNullOrEmpty(result)) { result = HttpContext.Current.Request.UserHostAddress; } return result; } catch { return "无法解析IP"; } } /// <summary> /// 获取服务器IP、Mac地址 /// </summary> /// <returns></returns> public static List<string> GetHostIPAndMac() { try { List<string> list = new List<string>(); System.Net.IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList; if (addressList.Length > 1) { list.Add(addressList[0].ToString()); list.Add(addressList[1].ToString()); } else { list.Add(addressList[0].ToString()); list.Add("没有可用的连接"); } return list; } catch (Exception) { return null; } } /// <summary> /// 获取库户端请求的Url /// </summary> /// <returns></returns> public static string GetClientUrl() { if(HttpContext.Current == null) { return "无法获取,该日志不是来源于Web"; } try { string result = HttpContext.Current.Request.Url.ToString(); if (string.IsNullOrEmpty(result)) { result = HttpContext.Current.Request.ServerVariables["Path_Info"]; } if (string.IsNullOrEmpty(result)) { result = HttpContext.Current.Request.UserHostAddress; } return result; } catch (Exception) { return "获取失败"; } } }}
[]
- ››iPhone应用帮助残障儿童看图说话
- ››iPhone实用工具AppBox Pro使用教程大揭秘
- ››IPad/IPhone 开发中的布局问题
- ››iPad2和iPhone将为苹果明年带来丰收
- ››iphone4省电方法
- ››iphone 获取地址的详细信息
- ››iPhone 库的基本内存管理策略
- ››iPhone加密文字亲手做 私密信息有保障
- ››iphone 根据经纬度坐标取详细地址(包括国,省,市...
- ››iphone/ipad ios cocoa object-c 近期苹果UI部分小...
- ››iphone中如何进行多线程编程
- ››iPhone OS SDK的这些事[安装、下载、版本、实例、...
更多精彩
赞助商链接