/// <summary> /// 获得公网IP /// </summary> /// <returns></returns> public static String GetPublicIP() { Uri uri = new Uri("http://www.ip138.com/ip2city.asp"); WebRequest wr = WebRequest.Create(uri); Stream s = wr.GetResponse().GetResponseStream(); StreamReader sr = new StreamReader(s, System.Text.Encoding.Default); string all = sr.ReadToEnd(); //读取网站的数据 int i = all.IndexOf("[") + 1; string tempip = all.Substring(i, 15); string ip = tempip.Replace("]", "").Replace(" ", "");//找出ip sr.Close(); s.Close(); return ip; }