c#获得客户端IP地址

前段时间做了个获取IP的例子,代码是百度上找的,因为是新手,写了几遍还是不太理解,在这里分享一下。

代码
#region GetIp 获得IP地址
public static string GetIp()
{
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
{
return System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].
Split(
new char[] { ',' })[0];
}
else
{
return System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
}
#endregion

 

posted @ 2011-01-04 16:08  序猿·徐  阅读(381)  评论(0编辑  收藏  举报