li.的博客
|
/// <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;
}