C#根據(jù)IP地址查詢所屬地區(qū)實(shí)例詳解
ip-api.com接口(解析 json需要引入Newtonsoft.Json.dll ):
/// <summary>
/// 根據(jù)IP 獲取物理地址
/// </summary>
/// <param name="ip">Ip地址</param>
/// <returns></returns>
public static string GetIpAddress(string ip)
{
string url = "http://ip-api.com/json/"+ip+"?lang=zh-CN";
string result = "";
WebRequest wrt = null;
WebResponse wrp = null;
try
{
wrt = WebRequest.Create(url);
wrt.Credentials = CredentialCache.DefaultCredentials;
wrp = wrt.GetResponse();
StreamReader sr = new StreamReader(wrp.GetResponseStream(), Encoding.UTF8);
//獲取到的是Json數(shù)據(jù)
string html = sr.ReadToEnd();
//Newtonsoft.Json讀取數(shù)據(jù)
JObject obj = JsonConvert.DeserializeObject<JObject>(html);
string city = obj["city"].ToString();
string province = obj["regionName"].ToString();
result = city.Equals(province) ? city : (province + city);
}
catch (Exception)
{
}
finally
{
if (wrp != null)
wrp.Close();
if (wrt != null)
wrt.Abort();
}
return result;
}
126.net接口:
/// <summary>
/// 根據(jù)IP 獲取物理地址
/// </summary>
/// <param name="ip">Ip地址</param>
/// <returns></returns>
public static string GetstringIpAddress(string ip)
{
string url = "http://ip.ws.126.net/ipquery?ip="+ip;
string result="";
WebRequest wrt = null;
WebResponse wrp = null;
try
{
wrt = WebRequest.Create(url);
wrt.Credentials = CredentialCache.DefaultCredentials;
wrp = wrt.GetResponse();
StreamReader sr = new StreamReader(wrp.GetResponseStream(), Encoding.Default);
//獲取到的數(shù)據(jù)格式:var lo="江蘇省", lc="鎮(zhèn)江市"; var localAddress={city:"鎮(zhèn)江市", province:"江蘇省"}
string html = sr.ReadToEnd();
string pattern = "{city:\"(?<key1>.*?)\", province:\"(?<key2>.*?)\"}";
Regex regex = new Regex(pattern, RegexOptions.None);
Match match = regex.Match(html);
string city=match.Groups["key1"].Value;
string province=match.Groups["key2"].Value;
result = city.Equals(province) ? city : (province + city);
}
catch (Exception)
{
}
finally
{
if (wrp != null)
wrp.Close();
if (wrt != null)
wrt.Abort();
}
return result;
}
到此這篇關(guān)于C#根據(jù)IP地址查詢所屬地區(qū)實(shí)例詳解的文章就介紹到這了,更多相關(guān)C#根據(jù)IP地址查詢所屬地區(qū)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c#發(fā)送請(qǐng)求訪問外部接口的實(shí)例
這篇文章主要介紹了c#發(fā)送請(qǐng)求訪問外部接口的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
C# 實(shí)現(xiàn)ADSL自動(dòng)斷網(wǎng)和撥號(hào)的方法(適用于撥號(hào)用戶)
下面小編就為大家?guī)硪黄狢# 實(shí)現(xiàn)ADSL自動(dòng)斷網(wǎng)和撥號(hào)的方法(適用于撥號(hào)用戶)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12
C#實(shí)現(xiàn)將TextBox綁定為KindEditor富文本
KindEditor?依靠出色的用戶體驗(yàn)和領(lǐng)先的技術(shù)提供富文本編輯功能,是一款非常受歡迎的HTML在線編輯器,下面我們就來看看C#如何將TextBox綁定為KindEditor富文本吧2024-04-04
C#請(qǐng)求唯一性校驗(yàn)支持高并發(fā)的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于C#請(qǐng)求唯一性校驗(yàn)支持高并發(fā)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
C#實(shí)現(xiàn)Word轉(zhuǎn)換TXT的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)Word轉(zhuǎn)換TXT的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
C#中parallel.foreach實(shí)現(xiàn)多線程處理
Parallel.ForEach方法是C#中的一個(gè)并行循環(huán)方法,它可以并行地對(duì)一個(gè)集合進(jìn)行迭代操作,本文主要介紹了C#中parallel.foreach實(shí)現(xiàn)多線程處理,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02

