WEB开发网
开发学院软件开发C语言 在DLL中获取客户端的IP和URL 阅读

在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 "获取失败";            }        }    }}

用DEF文件从DLL中导出C++类

http://tech.cncms.com/develop/cjj/42201.html

Tags:DLL 获取 客户端 IP URL

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接