c#得到本机内网ip、外网ip

2021-04-19 02:26

阅读:477

标签:ade   公网   one   bre   response   end   new   close   eth   

内网
 IPAddress ipAddr = Dns.Resolve(Dns.GetHostName()).AddressList[0];//获得当前IP地址
            string ip = ipAddr.ToString();

外网

 //获取本机的公网IP
        public static string GetPublicNetworkIP()
        {
            string tempip = "";
            WebRequest request = WebRequest.Create("http://ip.qq.com");
            request.Timeout = 10000;
            WebResponse response = request.GetResponse();
            Stream resStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
            string htmlinfo = sr.ReadToEnd();
            //匹配IP的正则表达式
            Regex r = new Regex("((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|[1-9])", RegexOptions.None);
            Match mc = r.Match(htmlinfo);
            //获取匹配到的IP
            tempip = mc.Groups[0].Value;
            resStream.Close();
            sr.Close();
            return tempip;
        }

 

c#得到本机内网ip、外网ip

标签:ade   公网   one   bre   response   end   new   close   eth   

原文地址:https://www.cnblogs.com/gaocong/p/8677133.html


评论


亲,登录后才可以留言!