复制代码 代码如下:
/// <summary>
/// 得到真实IP以及所在地详细信息(Porschev)
/// </summary>
/// <returns></returns>
public string GetIpDetails()
{
//设置获取IP地址和国家源码的网址
string url = "http://www.ip138.com/ips8.asp";
string regStr = "(?<=<td\\s*align=\\\"center\\\">)[^<]*?(?=<br/><br/></td>)";
//IP正则
string ipRegStr = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
//IP地址
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
ip = ma.Value;
int index = html.LastIndexOf(":") + 1;
//得到国家
country = html.Substring(index);
adr = GetAdrByIp(ip);
return "IP:" + ip + " 国家:" + country + " 省市:" + adr;
}
/// <summary>
/// 通过IP得到IP所在地省市(Porschev)
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public string GetAdrByIp(string ip)
{
string url = "http://www.cz88.net/ip/?ip=" + ip;
string regStr = "(?<=<span\\s*id=\\\"cz_addr\\\">).*?(?=</span>)";
//得到网页源码
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];
}

复制代码 代码如下:
/// <summary>
/// 获取HTML源码信息(Porschev)
/// </summary>
/// <param name="url">获取地址</param>
/// <returns>HTML源码</returns>
public string GetHtml(string url)
{
string str = "";
try
{
Uri uri = new Uri(url);
WebRequest wr = WebRequest.Create(uri);
Stream s = wr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
str = sr.ReadToEnd();
}
catch (Exception e)
{
}
return str;
}
标签:
查ip所在地,ip获取城市

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“C# 根据ip获取城市等相关信息”

暂无“C# 根据ip获取城市等相关信息”评论...