局域网中的主算机如何取外网IP地址? 代码 谢谢。

解决方案 »

  1.   

    string html = Encoding.GetEncoding(936).GetString(new WebClient().DownloadData("http://www.7y8.com/V/ip.asp"));
    string ip = Regex.Match(html, @"(?<=你的IP是\D+)[0-9.]+").Value;
      

  2.   

    static string GetPage(string url)  
      {  
      HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);  
      try  
      {  
      using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())  
      {  
      using (StreamReader sr = new StreamReader(res.GetResponseStream()))  
      {  
      return sr.ReadToEnd();  
      }  
      }  
      }  
      catch (System.Exception e)  
      {  
      return e.Message;  
      }  
      finally  
      {  
      req.Abort();  
      }  
      }  
      // 通过外部网站得到本机的外部IP  
      static string GetOuterIP()  
      {  
      string patt = @"IP: \[(? <IP>[0-9\.]*)\]";  
      string url = "";  
      return Regex.Match(GetPage(url), patt).Groups["IP"].Value;  
      }    static void Main()  
      {  
      foreach (IPAddress ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList)  
      {  
      Console.WriteLine(ip);  
      }  
      Console.WriteLine();  
      Console.WriteLine(GetOuterIP());  
      }
    http://topic.csdn.net/u/20100514/10/c856bd9f-e860-4430-990e-a6f2f80694f1.html
      

  3.   

    http://www.7y8.com/V/ip.asp
    打开看下