C# 根據(jù)ip獲取城市等相關(guān)信息
更新時(shí)間:2013年01月24日 16:57:33 作者:
本文將實(shí)現(xiàn):得到真實(shí)IP以及所在地詳細(xì)信息/通過(guò)IP得到IP所在地省市/獲取HTML源碼信息,感興趣的朋友不妨了解一下,或許對(duì)你鞏固所學(xué)知識(shí)有所幫助
復(fù)制代碼 代碼如下:
/// <summary>
/// 得到真實(shí)IP以及所在地詳細(xì)信息(Porschev)
/// </summary>
/// <returns></returns>
public string GetIpDetails()
{
//設(shè)置獲取IP地址和國(guó)家源碼的網(wǎng)址
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;
//國(guó)家
string country = string.Empty;
//省市
string adr = string.Empty;
//得到網(wǎng)頁(yè)源碼
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;
//得到國(guó)家
country = html.Substring(index);
adr = GetAdrByIp(ip);
return "IP:" + ip + " 國(guó)家:" + country + " 省市:" + adr;
}
/// <summary>
/// 通過(guò)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>)";
//得到網(wǎng)頁(yè)源碼
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];
}
復(fù)制代碼 代碼如下:
/// <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;
}
相關(guān)文章
asp.net 動(dòng)態(tài)表單之?dāng)?shù)據(jù)分頁(yè)
我們很常會(huì)在項(xiàng)目中提到一個(gè)動(dòng)態(tài)表單的概念,比如學(xué)校里面學(xué)生的考試成績(jī),當(dāng)學(xué)生登錄系統(tǒng)的時(shí)候,他當(dāng)然是希望看到他自己所有科目的成績(jī);又或者是班主任,他需要看到本班同學(xué)所有科目的成績(jī);這些時(shí)候我們一般都會(huì)在頁(yè)面中呈現(xiàn)如下的效果。2010-03-03asp.net Cookie值中文亂碼問(wèn)題解決方法
cookie里面不能寫中文,是由于cookie先天的編碼方式造成的,所以有必要存在一種中間的編碼方式:URLEncode是最好的選擇,感興趣的你可千萬(wàn)不要錯(cuò)過(guò)了哈,或許本文提供的知識(shí)點(diǎn)對(duì)你學(xué)習(xí)cookie有所幫助2013-02-02ASP.NET?MVC5實(shí)現(xiàn)文件上傳與地址變化處理(5)
這篇文章主要介紹了ASP.NET?MVC5實(shí)現(xiàn)文件上傳與地址變化處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09.net開(kāi)發(fā):為程式碼加上行號(hào)的方法詳解
這篇文章介紹了.net開(kāi)發(fā):為程式碼加上行號(hào)的方法,有需要的朋友可以參考一下2013-11-11WPF實(shí)現(xiàn)流光動(dòng)畫(huà)特效
這篇文章介紹了WPF實(shí)現(xiàn)流光動(dòng)畫(huà)特效的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04.Net Core簡(jiǎn)單使用Mvc內(nèi)置的Ioc
這篇文章主要為大家詳細(xì)介紹了.Net Core簡(jiǎn)單使用Mvc內(nèi)置的Ioc,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03ASP.NET WebForm中<%=%>與<%#%>的區(qū)別
這篇文章主要介紹了ASP.NET WebForm中<%=%>與<%#%>的區(qū)別,需要的朋友可以參考下2015-01-01