使用WebClient访问可以获取IP的网站,通过正则表达式抓取页面中的IP.
WebClient client = new WebClient();
byte[] bytRecv = client.DownloadData( "http://get-ip.me/");//下载网页数据
string str = System.Text.Encoding.UTF8.GetString(bytRecv);
string r = @"((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))";
string ip = System.Text.RegularExpressions.Regex.Match(str, r).ToString();//提取信息