C#/复杂网络环境,获取自己*IP

static string GetIP()
        {
            string strUrl = "http://city.ip138.com/city0.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, 14);//在这里要根据自己的实际情况来改了。前面是截取[,后面你可以截取]之后所有的字符串,留下中间的。
            string ip = tempip.Replace("]", "").Replace(" ", "");
            return ip;
        }
 int j=all.IndexOf("]")-1;
string tempip=all.Substring(i,j);
原文地址:https://www.cnblogs.com/Archosaur/p/IPAddress.html