Results 1 to 3 of 3

Thread: [RESOLVED] find External IP Address in VB.NET

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    166

    Resolved [RESOLVED] find External IP Address in VB.NET

    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...
    Code:
     Dim IPHost As IPHostEntry = Dns.GetHostByName(Dns.GetHostName())
     MessageBox.Show("My IP address is " & IPHost.AddressList(0).ToString())

    and i tried this code too...
    Code:
    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.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    166

    Re: find External IP Address in VB.NET

    I tried this code and it's working....

    Code:
    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())

  3. #3
    Lively Member turbotec's Avatar
    Join Date
    Aug 2007
    Posts
    102

    Re: [RESOLVED] find External IP Address in VB.NET

    I had to slightly modify your code but it works great. here is what worked for me in VB 2005

    Code:
    Dim req As Net.WebRequest = Net.WebRequest.Create("http://whatismyip.com/automation/n09230945.asp")
            Dim res As Net.WebResponse = req.GetResponse()
            Dim Stream As IO.Stream = res.GetResponseStream()
            Dim sr As IO.StreamReader = New IO.StreamReader(Stream)
            MessageBox.Show(sr.ReadToEnd())

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width