日期:2014-05-18  浏览次数:20798 次

c#如何编写一个程序查看自己的外网IP地址
我现在是局域网。我使用了下面代码查到的是内网的IP地址
System.Net.IPAddress[] ips;
ips = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName());
textBox1.Text = ips[2].ToString();




请问怎么编写查询外网IP地址的代码呢? 谢谢!

------解决方案--------------------
访问 www.ip138.com
httprequest 抓取上面的数据。
------解决方案--------------------
http://www.google.com.hk/search?hl=zh-CN&newwindow=1&safe=strict&biw=1260&bih=837&q=httpwebrequest+ip138+using&aq=f&aqi=&aql=&oq=
------解决方案--------------------
C# code

 string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了   
            Uri uri = new Uri(strUrl);   
            WebRequest wr = WebRequest.Create(uri);   
            Stream s = wr.GetResponse().GetResponseStream();   
            StreamReader sr = new StreamReader(s, Encoding.Default);   
            string all = sr.ReadToEnd(); //读取网站的数据   
            int i = all.IndexOf("[") + 1;   
            string tempip = all.Substring(i, 15);   
            string ip = tempip.Replace("]", "").Replace(" ", "");   
            return ip;