Question find External IP Address

remya1000

Well-known member
Joined
Mar 29, 2007
Messages
122
Programming Experience
Beginner
i'm using VB.NET 2003 Application program. i need to get External IP Address (internet). i searched internet and found come codes and tried that...

i tried this code... but it returned my internal IP Address...
VB.NET:
 Dim IPHost As IPHostEntry = Dns.GetHostByName(Dns.GetHostName())
 MessageBox.Show("My IP address is " & IPHost.AddressList(0).ToString())


and i tried this code too...
VB.NET:
Imports System
Imports System.Text
Imports System.Text.RegularExpressions

Public Sub GetExternalIP()
        Dim whatIsMyIp As String = "http://whatismyip.com"
        Dim getIpRegex As String = "(?<=<TITLE>.*)\d*\.\d*\.\d*\.\d*(?=</TITLE>)"
        Dim wc As WebClient = New WebClient
        Dim utf8 As UTF8Encoding = New UTF8Encoding
        Dim requestHtml As String = ""
        Dim externalIp As IPAddress = Nothing

        requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp))
        
        Dim r As Regex = New Regex(getIpRegex)
        Dim m As Match = r.Match(requestHtml)
        If (m.Success) Then
            externalIp = IPAddress.Parse(m.Value)
            MessageBox.Show(externalIp.ToString)
        End If
End Sub

but its always returns (m.fail) instead of (m.Success). so i'm not able to get External Ip address.

using command window - Immediate, i get values for "r" and "m". and m.success = false...
? r
{System.Text.RegularExpressions.Regex}
Options: None
RightToLeft: False

? r.Match(requestHtml)
{System.Text.RegularExpressions.Match}
Captures: {System.Text.RegularExpressions.CaptureCollection}
Empty: {System.Text.RegularExpressions.Match}
Groups: {System.Text.RegularExpressions.GroupCollection}
Index: 0
Length: 0
Success: False
Value: ""

i don't have fire wall setup in my machine. and i have internet access too...

anything wrong in that code.. or anything i'm missing... if anyone have any idea how to find out the External Ip Address, please help me. if you can provide an example, then it will be a great help for me.

Thanks in advance.
 
Use WebClient to download the string from http://whatismyip.org/ instead, the reply is the ip string and nothing more.
 
Thank John, it worked... its working....

I tried this code....

VB.NET:
Dim req As HttpWebRequest = WebRequest.Create("http://whatismyip.com/automation/n09230945.asp")
        Dim res As HttpWebResponse = req.GetResponse()
        Dim Stream As Stream = res.GetResponseStream()
        Dim sr As StreamReader = New StreamReader(Stream)
        messagebox.show(sr.ReadToEnd())

thanks a lot for this help....
 
Back
Top