通过正则抓取IP显示网站中的数据,并筛选出所需要的内容即可。
///
///
///
public string GetIpDetails()
{
string url = “http://www.ip138.com/ips8.asp”; //设置获取IP地址和国家源码的网址
string regStr = “(?<=
)”;
string ipRegStr = “((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\来源gao*daima.com搞@代#码网d|25[0-5]|[01]?\\d\\d?)”; //IP正则
string ip = string.Empty; //IP地址
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; //得到IP
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)
{
string url = “http://www.cz88.net/ip/?ip=” + ip;
string regStr = “(?<=).*?(?=)”;
string html = GetHtml(url); //得到网页源码
Regex reg = new Regex(regStr, RegexOptions.None);
Match ma = reg.Match(html);
html = ma.Value;
string[] arr = html.Split(”);
return arr[0];
}
#endregion
#region##获取HTML源码信息
///
///
///获取地址
///HTML源码
public string GetHtml(string url)
{
Uri uri = new Uri(url);
WebRequest wr = WebRequest.Create(uri);
Stream s = wr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
return sr.ReadToEnd();
}
#endregion
以上就是得到真实外网IP、IP所在国家、省份、地区(小偷程序)的详细内容,更多请关注gaodaima搞代码网其它相关文章!