C#获取本机(公网)IP、所在地以及网络类型

 public static void GetPulicAddress(ref string pubip, ref string address)
        {
            try
            {
                string strUrl = "http://iframe.ip138.com/ic.asp";//获得IP的网址
                Uri uri = new Uri(strUrl);
                WebRequest webreq = WebRequest.Create(uri);
                Stream s = webreq.GetResponse().GetResponseStream();
                StreamReader sr = new StreamReader(s, Encoding.Default);
                string all = sr.ReadToEnd(); //读取网站返回的数据 格式:您的IP地址是:[x.x.x.x]
                
                int i = all.IndexOf("[") + 1;
                int adr = all.IndexOf("来自") + 3;
                string tempadd = string.Empty,tempip=string.Empty;
                tempip = all.Substring(i, 15);
                tempadd = all.Substring(adr, all.Length - adr);
                 
                pubip = tempip.Substring(0,tempip.IndexOf("]"));
                address = tempadd.Substring(0, tempadd.IndexOf("<"));
                           }
            catch (Exception ex) { ComMsg.MsgErr(ex.Message); }
        }

 

posted @ 2013-07-31 23:09  永远的菜鸟@me  阅读(1133)  评论(0编辑  收藏  举报