现在的位置: 首页 > 综合 > 正文

C# WinForm 下获取外网的IP地址

2016年12月28日 ⁄ 综合 ⁄ 共 481字 ⁄ 字号 评论关闭
        /// <summary>
        /// 获取外网IP地址
        /// </summary>
        public static string GetIPAddress()
        {
            string sValue = String.Empty;
            string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址  
            Uri uri = new Uri(strUrl);
            WebRequest wr = WebRequest.Create(uri);
            Stream s = wr.GetResponse().GetResponseStream();
            StreamReader sr = new StreamReader(s, Encoding.Default);
            string all = sr.ReadToEnd(); //读取网站的数据  
            Match match;
            string pattern = "(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)";
            match = Regex.Match(all, pattern, RegexOptions.IgnoreCase);
            return match.ToString();
        }

 

抱歉!评论已关闭.