首页 > 其他 > 详细

常用的工具类4-IP类

时间:2016-06-22 01:36:50      阅读:96      评论:0      收藏:0      [点我收藏+]

public static class IpHelper
{
/// <summary>
/// 获取Ip
/// </summary>
/// <returns></returns>
public static string GetIp()
{
string[] IP_Ary;
string strIP, strIP_list;
strIP_list = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (strIP_list != null && strIP_list != "")
{
strIP_list = strIP_list.Replace("‘", "");
if (strIP_list.IndexOf(",") >= 0)
{
IP_Ary = strIP_list.Split(‘,‘);
strIP = IP_Ary[0];
}
else
{
strIP = strIP_list;
}
}
else
{
strIP = "";
}
if (strIP == "")
{
strIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
strIP = strIP.Replace("‘", "");
}
return strIP;
}

private static double IpToNum(string ip)
{
int s1 = 1;
int s2 = 256 * s1;
int s3 = 256 * s2;
int s4 = 256 * s3;
string[] IpNs = ip.Trim().Split(‘.‘);
try
{
double IpN = Convert.ToDouble(IpNs[0]) * s4 + Convert.ToDouble(IpNs[1]) * s3 + IpNs[2].ToInt() * s2 + IpNs[3].ToInt() * s1;
return IpN;
}
catch (Exception)
{
return 0;
}
}
}

常用的工具类4-IP类

原文:http://www.cnblogs.com/zhshlimi/p/5605534.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!