获取公网IP

Posted 路人甲zzz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取公网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的主要内容,如果未能解决你的问题,请参考以下文章