我见过几种在.NET中检索外部IP地址的方法;我想知道的是,所有通常的变量都是相同的(互联网连接速度等),最快的代码用于获取您的外部IP地址?什么是获取外部IP地址的绝对最快方法?
这里是迄今为止我见过的速度最快:
Private Function GetExternalIP() As String
Dim m As Match = Match.Empty
Try
Dim wClient As New System.Net.WebClient
Dim strURL As String = wClient.DownloadString("https://www.google.com/search?q=my+ip")
Dim strPattern As String = "\b(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b"
' Look for the IP
m = Regex.Match(strURL, strPattern)
Catch ex As Exception
Debug.WriteLine(String.Format("GetExternalIP Error: {0}", ex.Message))
End Try
' Failed getting the IP
If m.Success = False Then Return "IP: N/A"
' Got the IP
Return m.value
End Function
我知道还有其他方法来获得外部IP为好,即使用WebBrowser
控制,以获取该报告您的IP一个页面,然后分析出来的结果,甚至是一些命令行的方法,如乱搞的:
nslookup myip.opendns.com. resolver1.opendns.com
有没有人带到运行自己的测试,以得到fastes时间t方法?
有趣。我使用Google作为源,因为它可能更有可能在将来“永远在那里”,而不是随时可能消失的网站。我看到很多IP网站都会离开,破坏你的应用程序。但我确实理解从一个页面获取信息的概念,该页面不包含大量其他源代码来解析。 –
我同意您对服务可用性的担忧。我将该网址保存在配置文件中以便于维护/更改 – Steve
如果只有Google会为我们提供一个简单的IP信息页面。 =) –