得到真实外网IP、IP所在国家、省份、地区
2014年03月03日
⁄ 综合
⁄ 共 1507字 ⁄ 字号
小 中 大
-
#region##得到真实IP以及所在地详细信息
-
-
-
-
-
public string GetIpDetails()
-
{
-
string url = "http://www.ip138.com/ips8.asp"; //设置获取IP地址和国家源码的网址
-
string regStr = "(?<=<td\\s*align=\\\"center\\\">)[^<]*?(?=<br/><br/></td>)";
-
string ipRegStr = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
-
string ip = string.Empty;
-
string country = string.Empty;
-
string adr = string.Empty;
-
string html = GetHtml(url);
-
Regex reg = new Regex(regStr, RegexOptions.None);
-
Match ma = reg.Match(html); html = ma.Value;
-
Regex ipReg = new Regex(ipRegStr, RegexOptions.None);
-
ma = ipReg.Match(html);
-
ip = ma.Value;
-
int index = html.LastIndexOf(":") + 1;
-
country = html.Substring(index);
-
adr = GetAdrByIp(ip);
-
return "IP:" + ip + " 国家:" + country + " 省市:" + adr;
-
}
-
#endregion
-
-
#region##通过IP得到IP所在地省市
-
-
-
-
-
-
public string GetAdrByIp(string ip)