获取指定url的IP地址

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 }

你可能感兴趣的:(IP地址)