2012-07-29 26 views
0

我也嘗試使用GetIp方法(下)獲得互聯網IP得到正確的因特網IP,但只獲得一個IP這123.22.114.5,我重新啓動我的調制解調器很多次,但它仍然123.22.114.5,請幫助我。 (我使用Visual Studio 2010在Windows 7 64位)不能使用C#

(約我的英語不好對不起)

private string GetIp() 
    { 
     string extenalIp = ""; 
     string whatIsMyIp = "http://automation.whatismyip.com/n09230945.asp"; 
     string getIpRegex = @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"; 
     WebClient wc = new WebClient(); 
     UTF8Encoding utf8 = new UTF8Encoding(); 
     string requestHtml = ""; 
     try 
     { 
      requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp)); 
     } 
     catch (Exception e) 
     { 
      // do some thing with exception 
      Console.WriteLine(e.Message); 
     } 
     Regex r = new Regex(getIpRegex); 
     Match m = r.Match(requestHtml); 
     if (m.Success) 
     { 
      extenalIp = m.Value.ToString(); 
     } 
     wc.Dispose(); 
     return extenalIp; 
    } 
+0

你需要獲得外部IP,這個問題有幾個解決方案:http://stackoverflow.com/questions/3253701/c-sharp-get-public-external-ip-address – 2012-07-29 08:06:36

+0

是什麼讓你覺得123.22 .114.5不是您的互聯網IP嗎? – 2012-07-29 08:13:57

+0

謝謝cheeseweasel – tainguyen 2012-07-29 08:16:05

回答

1

你會得到的只是一個IP是你的ISP的外部 IP地址,當你請求一些外部網站如whatismyip

所有的ISP都有有限的靜態IP地址和麪向互聯網的主機。 這個廣域網IP地址通常被許多用戶共享。

您在那裏看到的IP與您的本地IP地址完全不同,因此,重新啓動您的調制解調器不會改變它。

+0

有些身體可以幫我 – tainguyen 2012-07-29 16:09:42

+0

您試圖檢索哪個IP? – 2012-07-29 16:13:07

+0

我想檢索像我在whatismyip – tainguyen 2012-07-30 03:06:00