2011-09-08 123 views

回答

1

您可以使用http://showip.codebrainz.ca/來獲得它。更多信息here

代碼示例:

using System; 
using System.Net; 

class Program 
{ 
    static void Main() 
    { 
    // Create web client. 
    WebClient client = new WebClient(); 

    // Download string. 
    string ip = client.DownloadString("http://showip.codebrainz.ca/"); 

    // Write ip. 
    Console.WriteLine(ip); 
    } 
} 
+0

這對我沒有用我想要得到我的路由器IP – Ayush

+0

這給你你的PUBLIC IP。 – rickythefox

0

獲取公網IP地址的最簡單的方法已經由其他海報解決。有一些替代方案,但它們是情境性的(如需要支持SNMP的路由器的SNMP)。

如果你想要一種方法,它不僅可以讓你獲得公共IP地址,還可以測試你如何連接到互聯網(例如對稱NAT,全錐體,直接連接等),你可以使用STUN協議(見RFC 5389)。 STUN還要求您聯繫外部STUN服務器。

如果您想要示例,Ivar Lumi在codeproject上發佈了一些working c# code。根據您的要求,這可能是矯枉過正。

+0

我得到了ans我的問題 – Ayush

+0

WebClient client = new WebClient(); client.Headers.Add(「user-agent」,「Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.2; .NET CLR1.0.3705;)」); string baseurl =「http://checkip.dyndns.org/」; 流數據= client.OpenRead(baseurl); StreamReader reader = new StreamReader(data); string s = reader.ReadToEnd(); data.Close(); reader.Close(); S = s.Replace( 「當前IP檢查」, 「」).Replace( 「」, 「」)的ToString(); MessageBox.Show(s); – Ayush

+0

基本上其他海報寫的 - 你應該upvote他們的答案;) – Anthill

相關問題