C#获取内网和外网IP

写了个小客户端,里面用到了获取内网和外网的IP地址,代码如下:

            // InnerIP
            var ipHost = Dns.Resolve(Dns.GetHostName());
            var ipaddress = ipHost.AddressList[0];
            innerIP = ipaddress.ToString();   
        /// <summary>
        /// 获得客户端外网IP地址
        /// </summary>
        /// <returns>IP地址</returns>
        private string GetClientInternetIP()
        {
            string ip;
            using (WebClient webClient = new WebClient())
            {
                var content = webClient.DownloadString("http://www.ip138.com/ips1388.asp"); //站获得IP的网页
                //判断IP是否合法
                ip = new Regex(@"\[((\d{1,3}\.){3}\d{1,3})\]").Match(content).Groups[1].Value;
            }
            return ip;
        }

 

posted @ 2015-03-05 12:22 pmars 阅读(...) 评论(...) 编辑 收藏