62,046
社区成员
发帖
与我相关
我的任务
分享
#region IP地址互转整数
/// <summary>
/// 将IP地址转为整数形式
/// </summary>
/// <returns>整数</returns>
public static long IP2Long(IPAddress ip)
{
int x = 3;
long o = 0;
foreach (byte f in ip.GetAddressBytes())
{
o += (long)f << 8 * x--;
}
return o;
}
/// <summary>
/// 将整数转为IP地址
/// </summary>
/// <returns>IP地址</returns>
public static IPAddress Long2IP(long l)
{
byte[] b = new byte[4];
for (int i = 0; i < 4; i++)
{
b[3 - i] = (byte)(l >> 8 * i & 255);
}
return new IPAddress(b);
}
#endregion
/// <summary>
/// 获得客户端IP
/// </summary>
public static string ClientIP
{
get
{
string ip;
string[] temp;
bool isErr = false;
if (HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"] == null)
ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
else
ip = HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"].ToString();
if (ip.Length > 15)
isErr = true;
else
{
temp = ip.Split('.');
if (temp.Length == 4)
{
for (int i = 0; i < temp.Length; i++)
{
if (temp[i].Length > 3) isErr = true;
}
}
else
isErr = true;
}
if (isErr)
return "1.1.1.1";
else
return ip;
}
}
//string ClientIp = BoardGame_Tools.UserLoginHelp.GetOuterIP("http://checkip.dyndns.org");
//string ClientIpAddress = BoardGame_Tools.UserLoginHelp.GetIpAddress("http://checkip.dyndns.org");
#region 读取IP、IP地址
/*
*有道(yodao)提供的接口返回给我们是xml格式的文件,
*在IE中输入网址:http://www.youdao.com/smartresult-xml/search.s?type=ip&q=59.58.2.171,得到的格式文件就是:
<?xml version="1.0" encoding="gbk" ?>
<smartresult>
<product type="ip">
<ip>59.58.2.171</ip>
<location>福建省宁德市 (古田县)电信</location>
</product>
</smartresult>
*我们真正需要只有<location>福建省宁德市 (古田县)电信</location> 这个
*/
/// <summary>
/// 读取http://www.yodao.com接口IP地址
/// </summary>
public static string GetIpAddress(string paramURL)
{
string strIP = GetOuterIP(paramURL);
string sURL = "http://www.youdao.com/smartresult-xml/search.s?type=ip&q=" + strIP + "";
string stringIpAddress = "";
using (System.Xml.XmlReader read = System.Xml.XmlReader.Create(sURL))//获取youdao返回的xml格式文件内容
{
while (read.Read())
{
switch (read.NodeType)
{
case XmlNodeType.Text://取xml格式文件当中的文本内容
if (string.Format("{0}", read.Value).ToString().Trim() != strIP)
{//youdao返回的xml格式文件内容一个是IP,另一个是IP地址,如果不是IP那么就是IP地址
stringIpAddress = string.Format("{0}", read.Value).ToString().Trim();//赋值
}
break;
//other
}
}
}
return stringIpAddress;
}
/// <summary>
/// 通过外部网站得到本机的外部IP
/// </summary>
/// <param name="paramurl">能够显示本机外网ip的网址</param>
/// <returns></returns>
public static string GetOuterIP(string paramurl)
{
//string patt = @"IP: \[(?<IP>[0-9\.]*)\]";
//string url = "http://checkip.dyndns.org";
//string url = "http://www.ip138.com/";
//return Regex.Match(GetPage(paramurl), patt).Groups["IP"].Value;//此方法有问题啊,修改如下:
//<html><head><title>Current IP Check</title></head><body>Current IP Address: 61.190.40.110</body></html>
string a = GetPage(paramurl);
int i = a.IndexOf(":");//找a的位置
int j = a.IndexOf("</b");//找b的位置
return (a.Substring(i + 1)).Substring(0, j - i - 1);//找出:和</b之间的字符串
/*注:Regex.Match(string input, string patterm) 方法
在输入字符串中搜索正则表达式的匹配项,并将精确结果作为单个 Match 对象返回
input 为输入字符, pattern 参数中提供的正则表达式的匹配项*/
}
/// <summary>
/// 获取网页的HTML内容
/// </summary>
/// <param name="url">请求的url</param>
/// <returns>返回请求网页的信息</returns>
public static string GetPage(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
try
{
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
using (StreamReader sr = new StreamReader(res.GetResponseStream()))
{
return sr.ReadToEnd();
}
}
}
catch (System.Exception e)
{
return e.Message;
}
finally
{
req.Abort();
}
/*返回内容,例如:
* <html><head><title>Current IP Check</title></head><body>Current IP Address: 61.190.40.110</body></html>
*/
}
#endregion
IPHostEntry hostInfo = DNS.GetHostByName("www.baidu.com");
Response.Write(hostInfo.AddressList[0].ToString());
//需要引用 using System.Net;