string strUrl ="http://www.ip138.com/ip2city.asp";
//
獲得IP的網址了
Uri
uri =newUri(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, Encoding.Default);
string
all = sr.ReadToEnd(); //讀取網站的數據
string
IP =new System.Text.RegularExpressions.Regex(@"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}").Match(all).Value;
//Winform
取得外網IP只能使用訪問網頁來取得真實IP,下面的兩種方式都只能取得區網IP
//(1)

//IPHostEntry MyEntry = Dns.GetHostEntry(Dns.GetHostName());

//string IP = MyEntry.AddressList[0].ToString();

//(2)

//IPHostEntry MyEntry = Dns.Resolve(Dns.GetHostName());

//string IP = MyEntry.AddressList[0].ToString();

arrow
arrow
    全站熱搜

    souts 發表在 痞客邦 留言(0) 人氣()