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

        /// <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();
        }


 

你可能感兴趣的:(String,Stream,C#,WinForm)