C#获取本机公网IP

 1 /// <summary>
 2 /// 获取本机公网IP
 3 /// </summary>
 4 /// <returns></returns>
 5 public static string GetIP_Lan()
 6 {
 7     string tempip = "127.0.0.1";
 8     try
 9     {
10         WebRequest wr = WebRequest.Create("http://www.ip138.com/ip2city.asp");
11         Stream s = wr.GetResponse().GetResponseStream();
12         StreamReader sr = new StreamReader(s, Encoding.Default);
13         string all = sr.ReadToEnd(); 
14         int start = all.IndexOf("[") + 1;
15         int end = all.IndexOf("]", start);
16         tempip = all.Substring(start, end - start);
17         sr.Close();
18         s.Close();
19     }
20     catch
21     {
22     }
23     return tempip;
24 }

 

posted @ 2016-12-29 10:31  wuln  阅读(407)  评论(0编辑  收藏  举报