首页 > 代码库 > 获取公网IP

获取公网IP

        private static string GetIP()        {            string tempip = "";            try            {                WebRequest wr = WebRequest.Create("http://ip.myhostadmin.net/");                Stream s = wr.GetResponse().GetResponseStream();                StreamReader sr = new StreamReader(s, Encoding.Default);                string all = sr.ReadToEnd(); //读取网站的数据                all = all.Replace("class=\"STYLE1\">", "*");                all = all.Replace("</h1>", "&");                int start = all.IndexOf("*") + 1;                int end = all.LastIndexOf("&");                tempip = all.Substring(start, end - start);                sr.Close();                s.Close();            }            catch            {            }            return tempip;        }

  

获取公网IP