Results 1 to 14 of 14

Thread: VB.Net Simple function to get external IP-adress

  1. #1

    Thread Starter
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Cool VB.Net Simple function to get external IP-adress

    This is The Code
    Code:
    Imports System.Net
    Imports System.Text.RegularExpressions
    
     Function GetExternalIP() As IPAddress
            Dim lol As WebClient = New WebClient()
            Dim str As String = lol.DownloadString("http://www.ip-adress.com/")
            Dim pattern As String = "<h2>My IP address is: (.+)</h2>"
            Dim matches1 As MatchCollection = Regex.Matches(str, pattern)
            Dim ip As String = matches1(0).ToString
            ip = ip.Remove(0, 21)
            ip = ip.Replace("</h2>", "")
            ip = ip.Replace(" ", "")
            Return IPAddress.Parse(ip)
        End Function
    This is How to use it

    Code:
     Dim MyIP As IPAddress = GetExternalIP()
    This code is an easy method to get your Ip-address. I've read that sites disclaimer and all the fine print and nothing said I'm not allowed to parse it's html code, although I'm not sure if their is a daily limit or not

    This is my first code bank submission and I hope that it will help someone out
    I did a search and didn't find something similar
    Last edited by BlindSniper; Jan 6th, 2011 at 08:09 AM. Reason: added Imports

  2. #2
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: VB.Net Simple function to get external IP-adress

    A similar way but utilizing Regular Expressions built in features:

    vb Code:
    1. Private Function GetMyIP() As IPAddress
    2.         Dim outputIP As IPAddress
    3.         Using wClient As New WebClient
    4.             Dim myIP As String = Regex.Match(wClient.DownloadString("http://www.ip-adress.com/"), "(?<=<h2>My IP address is: )[0-9.]*?(?=</h2>)", RegexOptions.Compiled).Value
    5.             outputIP = IPAddress.Parse(myIP)
    6.         End Using
    7.         Return outputIP
    8.     End Function

    Or if you want to get ugly

    vb Code:
    1. Private Function GetMyIP() As IPAddress
    2.         Using wClient As New WebClient
    3.             Return IPAddress.Parse(Regex.Match(wClient.DownloadString("http://www.ip-adress.com/"), "(?<=<h2>My IP address is: )[0-9.]*?(?=</h2>)", RegexOptions.Compiled).Value)
    4.         End Using
    5.     End Function
    If I helped you out, please take the time to rate me

  3. #3
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: VB.Net Simple function to get external IP-adress

    Sorry for the double post but I felt I should share this, as it's a heck of a lot easier. Change your Imports to just System.Text and try the following.

    vb Code:
    1. Private Function GetMyIP() As Net.IPAddress
    2.         Using wc As New Net.WebClient
    3.             Return Net.IPAddress.Parse(Encoding.ASCII.GetString(wc.DownloadData("http://whatismyip.com/automation/n09230945.asp")))
    4.         End Using
    5.     End Function

    That returns your ip with no string formatting necessary.
    If I helped you out, please take the time to rate me

  4. #4
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: VB.Net Simple function to get external IP-adress

    Since the last code doens't work anymore and people might still want to use it, I have an alternative but simular solution:
    Code:
    Private Function GetMyIP() As Net.IPAddress
    	Using wc As New Net.WebClient
    	Return Net.IPAddress.Parse(Encoding.ASCII.GetString(wc.DownloadData("http://tools.feron.it/php/ip.php")))
    	End Using
    End Function
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  5. #5
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: VB.Net Simple function to get external IP-adress

    This is very simple, clever and cool, thanks for sharing!

  6. #6
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: VB.Net Simple function to get external IP-adress

    You're welcome, feron.it is my own domain and I'll keep this service running forever for free. It is just 2 line PHP
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  7. #7
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: VB.Net Simple function to get external IP-adress

    Could you write this in VBScript I'm not sure how to do this in VBScript. How to download and parse using VBScript

  8. #8
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: VB.Net Simple function to get external IP-adress

    Quote Originally Posted by a_ahmed View Post
    Could you write this in VBScript I'm not sure how to do this in VBScript. How to download and parse using VBScript
    You have posted in the wrong section for the language you want to use! VBscript is a different language to VB.NET. Please ask the question in a new thread in the ASP/VB Script section of the forum.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  9. #9
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: VB.Net Simple function to get external IP-adress

    Of course I know, but was hoping he would share a VBScript version of his shared code too

  10. #10
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: VB.Net Simple function to get external IP-adress

    Quote Originally Posted by a_ahmed View Post
    Of course I know, but was hoping he would share a VBScript version of his shared code too
    BlindSniper might not know VBScript though.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  11. #11
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    353

    Re: VB.Net Simple function to get external IP-adress

    I know I asked this before and I found a solution for vbscript:

    vb Code:
    1. Function MyIP_WinHTTP( )
    2.     Dim lngStatus, objHTTP, objMatch, objRE, strText, strURL
    3.     MyIP_WinHTTP = "0.0.0.0"
    4.     strURL = "http://tools.feron.it/php/ip.php"
    5.     Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
    6.     objHTTP.Open "GET", strURL
    7.     objHTTP.Send
    8.     If objHTTP.Status = 200 Then MyIP_WinHTTP = objHTTP.ResponseText
    9.     Set objHTTP = Nothing
    10. End Function

    then just call myIP_WinHTTP() and put in a variable or echo it

  12. #12
    New Member
    Join Date
    Oct 2016
    Posts
    4

    Re: VB.Net Simple function to get external IP-adress

    This can help

    Code:
        Public Structure DatosIPExterna
            Dim IP As String
            Dim CountryCode As String
            Dim CountryName As String
            Dim RegionCode As String
            Dim RegionName As String
            Dim City As String
            Dim TimeZone As String
            Dim Latitude As Double
            Dim Longitude As Double
        End Structure
    
    Public Function GeoIP(Optional IP As String = Nothing) As DatosIPExterna
            Dim DatosIP As New DatosIPExterna
    
            Using client = New WebClient()
                Try
                    Dim strFile As String
                    If IsNothing(IP) Then
                        strFile = client.DownloadString("http://freegeoip.net/xml")
                    Else
                        strFile = client.DownloadString(String.Format("http://freegeoip.net/xml/{0}", IP))
                    End If
    
                    Dim response As XElement = XElement.Parse(strFile)
    
                    With DatosIP
                        .IP = response.Element("IP").Value
                        .CountryCode = response.Element("CountryCode").Value
                        .CountryName = response.Element("CountryName").Value
                        .RegionCode = response.Element("RegionCode").Value
                        .RegionName = response.Element("RegionName").Value
                        .City = response.Element("City").Value
                        .TimeZone = response.Element("TimeZone").Value
                        .Latitude = CType(response.Element("Latitude").Value, Long)
                        .Longitude = CType(response.Element("Longitude").Value, Long)
                    End With
    
                Catch ex As Exception
                    Return Nothing
                End Try
            End Using
    
            Return DatosIP
    
        End Function

  13. #13
    New Member
    Join Date
    Aug 2020
    Posts
    1

    Re: VB.Net Simple function to get external IP-adress

    You're welcome, feron.it is my own domain and I'll keep this service running forever for free. It is just 2 line PHP
    @Lightning Cool page! I wouldn't mind hosting this as well, you mind sharing the ip.php code?
    Last edited by danielmaat; Aug 21st, 2020 at 08:47 AM.

  14. #14
    New Member
    Join Date
    Apr 2020
    Posts
    7

    Thumbs up Re: VB.Net Simple function to get external IP-adress

    first time caller, long time listener...

    Quote Originally Posted by danielmaat View Post
    @Lightning Cool page! I wouldn't mind hosting this as well, you mind sharing the ip.php code?
    simple... in Perl:
    #!/usr/bin/perl

    $number = $ENV{REMOTE_ADDR};
    print "Content-type: text/plain\n\n$number";
    1;
    #example url: https://www.serp2tab.com/ip.cgi
    In php:

    <?php
    echo 'User IP Address - '.$_SERVER['REMOTE_ADDR'];
    ?>
    #example url: https://www.serp2tab.com/ip.php

Tags for this Thread

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