码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET获取客户端IP地址

时间:2016-08-01 10:37:40      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

public static string GetRealIP()
        {
            string ip;
            try
            {
                HttpRequest request = HttpContext.Current.Request;

                if (request.ServerVariables["HTTP_VIA"] != null)
                {
                    ip = request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(‘,‘)[0].Trim();
                }
                else
                {
                    ip = request.UserHostAddress;
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return ip;
        }

ASP.NET获取客户端IP地址

标签:

原文地址:http://www.cnblogs.com/DONET-LC/p/5724805.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!