首页 > 代码库 > MVC 如何获取外网IP

MVC 如何获取外网IP

 1 public string GetIP()  2 { 3   string tempIp = ""; 4   WebRequest wr = WebRequest.Create("http://city.ip138.com/ip2city.asp"); 5   Stream s = wr.GetResponse().GetResponseStream(); 6   StreamReader sr = new StreamReader(s, Encoding.Default); 7   string all = sr.ReadToEnd(); 8   int start = all.IndexOf("[") + 1; 9   int end = all.IndexOf("]", start);10   tempIp = all.Substring(start, end - start);11   sr.Close();12   s.Close();13   return tempIp;14 }

这种获取IP的方式是根据 http://city.ip138.com/ip2city.asp 来获取的,最后已截取字符串结束

MVC 如何获取外网IP