public static string GetNetIP()
{
string tempIP = "";
try
{
System.Net.WebRequest wr = System.Net.WebRequest.Create("http://city.ip138.com/ip2city.asp");
System.IO.Stream s = wr.GetResponse().GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(s, System.Text.Encoding.GetEncoding("gb2312"));
string all = sr.ReadToEnd(); //读取网站的数据
int start = all.IndexOf("[") + 1;
int end = all.IndexOf("]", start);
tempIP = all.Substring(start, end - start);
sr.Close();
s.Close();
}
catch
{
if (System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.Length > 1)
tempIP = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[1].ToString();
if (string.IsNullOrEmpty(tempIP))
return GetIP();
}
return tempIP;
}