find External IP Address in VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • remya1000
    New Member
    • Apr 2007
    • 115

    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.Re gularExpression s.Regex}
    Options: None
    RightToLeft: False

    ? r.Match(request Html)
    {System.Text.Re gularExpression s.Match}
    Captures: {System.Text.Re gularExpression s.CaptureCollec tion}
    Empty: {System.Text.Re gularExpression s.Match}
    Groups: {System.Text.Re gularExpression s.GroupCollecti on}
    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.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Using a screen scraping method from a site like What's My Ip is probably the best way to do it.
    You would need to understand what was going on in the code to make that work.

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Use this page for WhatIsMyIP. They don't want you loading the front page programatically . The page I linked returns nothing but an IP. Here's why you should use that page:
      Originally posted by WhatIsMyIp.Com
      Some automators are hitting our home page really hard eating up a lot of bandwidth and possibly causing the site to be slow for other visitors. So we've created an Automation page. This file should make it cleaner for you and easier on our bandwidth. Even though it'll be lighter on the bandwidth, please ONLY hit this page at a reasonable pace. Some of you are hitting the site at a pace of 2 times per second...that's WAY TOO often. We prefer you only hit it once per every 5 minutes, but if you need it more often than that, contact us and we can work something out.
      Use this code:
      Code:
      using System.Net;
      using System.IO;
      .
      .
      .
      HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://whatismyip.com/automation/n09230945.asp");
      HttpWebResponse res = (HttpWebResponse)req.GetResponse();
      Stream stream = res.GetResponseStream();
      StreamReader sr = new StreamReader(stream);
      string ip = sr.ReadToEnd();
      The first step creates a web request to the linked page. Then you get the response, and get the stream from the response. Using a StreamReader, I read the stream to the end, and now I have an IP string.

      Comment

      • remya1000
        New Member
        • Apr 2007
        • 115

        #4
        Thank a lot to Plater and Insert Alias, that code worked... its 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())
        thanks a lot for this help.... thanks a lot.....

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          No problem. Glad we could help.

          Comment

          Working...