2016-09-17 11 views
0

我的方法获取的extern IP:它基本上返回的extern IPadressC#中使用的WebRequest需要很长时间

public static string Get_IP() 
{ 
    string url = "http://checkip.dyndns.org"; 
    WebRequest req = WebRequest.Create(url); 
    WebResponse resp = req.GetResponse(); 
    StreamReader sr = new StreamReader(resp.GetResponseStream()); 
    string response = sr.ReadToEnd().Trim(); 
    string[] a = response.Split(':'); 
    string a2 = a[1].Substring(1); 
    string[] a3 = a2.Split('<'); 
    string a4 = a3[0]; 
    return a4; 
} 

我不能明白,为什么我的方法需要很长的时间来加载IpAdress,有时它会立即回吧!

ps:我检查了我的互联网连接,它工作完美,我不认为它的连接速度问题!

谢谢!

+0

http://stackoverflow.com/questions/11794413/get-endpoint-ip-from-url – Oluwafemi

回答

2

随着这段代码这将是快速

  string externalip = new WebClient().DownloadString("http://icanhazip.com"); 
      Console.WriteLine(externalip); 
+0

谢谢主席先生! –

+0

@CruDy接受答案,如果它帮助你。 –

+0

您能否为未来的参考添加解释? – Caramiriel