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

常用的工具类4-IP类

时间:2016-06-22 01:39:06      阅读:125      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!