
Saturday, May 28, 2005
Previous Posts
Imports System.Net.WebClient Imports System.Net Imports System.Web Imports System.Text.RegularExpressions Imports System.IO Module Module1 ' Purpose: Get the external IP address of the computer. Using ' dyndns.org's checkip tool. This will work ' behind NAT routers. ' I: (none) ' O: external IP address Function GetExternalIP() As String Const IP_URL As String = "http://checkip.dyndns.org" Dim strHTML As String, strIP As String ' Send the request and hopefully we'll get response. ' Try to parse out the IP address from the returned HTML Try Dim objWebReq As WebRequest = _ WebRequest.Create(IP_URL) Dim objWebResp As WebResponse = _ objWebReq.GetResponse() Dim streamResp As Stream = _ objWebResp.GetResponseStream() Dim srResp As StreamReader = _ New StreamReader(streamResp, True) strHTML = srResp.ReadToEnd() Dim regexIP As Regex = _ New Regex("\b\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\b") strIP = regexIP.Match(strHTML).Value Return strIP Catch ex As Exception Console.WriteLine("Can't retrieve external IP: " + _ ex.Message) Return Nothing End Try End Function Sub Main() Console.WriteLine(GetExternalIP()) End Sub End Module
| |||||||
0 Comments:
Post a Comment
<< Home