C# .net 获取外网ip

public string GetIP()
    {
        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;

int j = all.IndexOf("]");

          string ip = all.Substring(i, j-i).Trim();

return ip;
    }
posted @ 2015-04-26 13:55  平民的麦田  阅读(1261)  评论(0编辑  收藏  举报