li.的博客
li.的首页 > li.的博客 > 浏览文章

C#获取客户端IP地址与IP地址转换

分类:.net应用  人气:1178  评论:0  时间:2009-12-24 08:52

/// <summary>
        /// 获取客户端真实IP
        /// </summary>
        /// <returns></returns>
        public static string GetClientIP()
        {
            string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            if (null == result || result == String.Empty)
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }
            return result;
        }

        /// <summary>
        /// IP地址转换
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static float IP2Num(string ip)
        {
            float ipnum = 0;

            if (ip != "")
            {
                string[] u = ip.Split('.');

                ipnum = float.Parse(u[0].ToString()) * 256 * 256 * 256 + float.Parse(u[1].ToString()) * 256 * 256 + float.Parse(u[2].ToString()) * 256 + float.Parse(u[3].ToString());
            }

            return ipnum;
        }

标签(Tag):C# ip REMOTE_ADDR 编程
评论(0)
暂无评论
我来评论
(800字以内)