码迷,mamicode.com
首页 > 其他好文 > 详细

获取客户端Ip地址方法

时间:2015-05-04 13:29:33      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

public static string GetIp()
{
string ip;

HttpRequest request = HttpContext.Current.Request;
if (request.ServerVariables.AllKeys.Contains("HTTP_X_FORWARDED_FOR"))
{
string httpXff = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!String.IsNullOrEmpty(httpXff) && !httpXff.Contains("unknown"))
{
string[] xffList = httpXff.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries);
if (xffList.Length >= 1)
{
return xffList[0];
}
}
}

string clientIp = request.ServerVariables["HTTP_CLIENT_IP"];
if (clientIp != null)
{
ip = clientIp;
}
else
{
string remoteAddress = request.ServerVariables["REMOTE_ADDR"];
ip = remoteAddress ?? "0.0.0.0";
}

return ip;
}

获取客户端Ip地址方法

标签:

原文地址:http://www.cnblogs.com/lxf1117/p/4475707.html

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