因項目需要,老大讓做一個類似QQ那樣能知道你的登陸地點的東西。
這下慌啊,。後來找到一個網站 www.ip138.com
進去看了一下驚喜啊,我要的東西里面都有。 可是,怎麼樣能將我想要的物理地址拿出來,放在我自己的項目中呢。
下面的代碼能解釋一切
try
{
string strUrl = "http://city.ip138.com/city.asp"; //獲得IP的網址了
Uri uri = new Uri(strUrl);
System.Net.WebRequest wr = System.Net.WebRequest.Create(uri);
System.IO.Stream s = wr.GetResponse().GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(s, System.Text.Encoding.Default);
string all = sr.ReadToEnd(); //讀取網站的數據
int i = all.IndexOf("[") + 1;
string tempip = all.Substring(i, 15);
ip = tempip.Replace("]", "").Replace(" ", "");//找出公網IP
int ii=all.LastIndexOf(":") + 1;
Address = all.Substring(ii, all.LastIndexOf(" ") - all.LastIndexOf(":") - 1);//找出物理地址,如:江蘇省蘇州市
label10.Text = "您當前所在的位置是:" + Address;
}
catch (Exception ex)
{
}
問題一下子解決,唉,終於鬆了口氣,那個叫心情舒暢啊