博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

获取指定url的IP地址

Posted on 2011-03-22 17:03  moss_tan_jun  阅读(488)  评论(0编辑  收藏  举报
1 static string GetIP()
2 {
//http://www.ip138.com/ip2city.asp
3 Uri uri = new Uri("http://www.ikaka.com/ip/index.asp");
4 System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
5 req.Method = "POST";
6 req.ContentType = "application/x-www-form-urlencoded";
7 req.ContentLength = 0;
8 req.CookieContainer = new System.Net.CookieContainer();
9 req.GetRequestStream().Write(new byte [0], 0, 0);
10 System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)(req.GetResponse());
11 StreamReader rs = new StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding("GB18030"));
12 string s = rs.ReadToEnd();
13 rs.Close();
14 req.Abort();
15 res.Close();
16 System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(s, @"IP:\[(?<IP>[0-9\.]*)\]");
17 if (m.Success) return m.Groups["IP"].Value;
18 return string.Empty;
19 }