none
Convert to code to work in visual studio 2013 RRS feed

  • Question

  • I was wondering if you could convert this old code to work in visual studio 2013

    Imports System.Net

            

    Public Class Form1
        Dim Client As New WebClient
        Dim city As String
        Dim Ip As String
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            GetCity()
            Test()
            TextBox1.AppendText("[Ip]-" + Ip + vbNewLine + "[City]-" + city)
        End Sub
        Private Function GetCity()

            Try
                Dim Page As String = Client.DownloadString("http://www.find-ip-address.org/")
                city = Page.Substring(Page.IndexOf("IP Address Lookup Location") + 80)
                city = city.Substring(0, city.IndexOf(" <"))

            Catch ex As Exception
                city = "Unable to lookup"
            End Try
        End Function
        Private Function getip()

            Try
                Dim Page As String = Client.DownloadString("http://www.find-ip-address.org/")
                Ip = Page.Substring(Page.IndexOf("My Ip Address") + 29)
                Ip = Ip.Substring(0, city.IndexOf(" </td") + 13)

            Catch ex As Exception
                city = "Unable to lookup"
            End Try
        End Function

    End Class


    Wednesday, June 7, 2017 9:29 PM

Answers

  • Ben,

    If you want the information then the following can help; if you want the info from that specific site, I won't help because that's all about "look at my Google ads" all over the place.

    There are better sites to get this info and one in particular is this one:

    http://ip-api.com/

    The API has a few formats that you can choose from and in the following, I've chosen to use XML, although JSON is smaller so you may want to consider that one.

    If you'll try the following in a new project, when the code gets to "Stop", hover your mouse over the variable named "data" and you'll see the information:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Dim data As ExternalIP_Data = New ExternalIP_Data
    
            Stop
    
        End Sub
    
    
    End Class
    
    
    
    
    
    Public Class ExternalIP_Data
        Private _externalIP As System.Net.IPAddress
        Private _country As String
        Private _countryCode As String
        Private _region As String
        Private _regionName As String
        Private _city As String
        Private _zipCode As String
        Private _latitude As Double
        Private _longitude As Double
        Private _timeZone As String
        Private _iSP As String
    
        Public Sub New()
    
            Dim xDoc As XElement = XElement.Load("http://ip-api.com/xml")
    
            If xDoc...<status>.Value = "success" Then
                _country = xDoc...<country>.Value
                _countryCode = xDoc...<countryCode>.Value
                _region = xDoc...<region>.Value
                _regionName = xDoc...<regionName>.Value
                _city = xDoc...<city>.Value
                _zipCode = xDoc...<zip>.Value
                _latitude = CDbl(xDoc...<lat>.Value)
                _longitude = CDbl(xDoc...<lon>.Value)
                _timeZone = xDoc...<timezone>.Value
                _iSP = xDoc...<isp>.Value
                _externalIP = System.Net.IPAddress.Parse(xDoc...<query>.Value)
            End If
    
        End Sub
    
        Public ReadOnly Property City As String
            Get
                Return _city
            End Get
        End Property
    
        Public ReadOnly Property Country As String
            Get
                Return _country
            End Get
        End Property
    
        Public ReadOnly Property CountryCode As String
            Get
                Return _countryCode
            End Get
        End Property
    
        Public ReadOnly Property ExternalIP As System.Net.IPAddress
            Get
                Return _externalIP
            End Get
        End Property
    
        Public ReadOnly Property ExternalIP_String As String
            Get
                Return _externalIP.ToString
            End Get
        End Property
    
        Public ReadOnly Property ISP As String
            Get
                Return _iSP
            End Get
        End Property
    
        Public ReadOnly Property Latitude As Double
            Get
                Return _latitude
            End Get
        End Property
    
        Public ReadOnly Property Latitude_String As String
            Get
                Return _latitude.ToString("f4")
            End Get
        End Property
    
        Public ReadOnly Property Longitude As Double
            Get
                Return _longitude
            End Get
        End Property
    
        Public ReadOnly Property Longitude_String As String
            Get
                Return _longitude.ToString("f4")
            End Get
        End Property
    
        Public ReadOnly Property Region As String
            Get
                Return _region
            End Get
        End Property
    
        Public ReadOnly Property RegionName As String
            Get
                Return _regionName
            End Get
        End Property
    
        Public ReadOnly Property TimeZone As String
            Get
                Return _timeZone
            End Get
        End Property
    
        Public ReadOnly Property ZipCode As String
            Get
                Return _zipCode
            End Get
        End Property
    End Class


    I hope that helps.


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Proposed as answer by IronRazerz Friday, June 9, 2017 12:49 AM
    • Edited by Frank L. Smith Friday, June 9, 2017 9:45 AM ...modified code to correct an error
    • Marked as answer by BadNewsHughes Friday, June 16, 2017 1:49 PM
    Thursday, June 8, 2017 11:20 PM
  •  @ Frank,

     Nice work.  It works good.  Just on thing,  there is one line that needs to be corrected,  this line...

    _city = xDoc...<regionName>.Value
     

     Should be...

    _city = xDoc...<city>.Value
     

     Also,  just as a quick way to get all the info into a Label or TextBox/RichTextBox if anyone else,  or you,  want to copy/paste it into the class...

        Public Overrides Function ToString() As String
            Dim sb As New System.Text.StringBuilder
            sb.AppendLine("IP:  " & ExternalIP_String)
            sb.AppendLine("Country:  " & _country)
            sb.AppendLine("Country Code:  " & _countryCode)
            sb.AppendLine("Region:  " & _regionName)
            sb.AppendLine("Region Code:  " & _region)
            sb.AppendLine("City:  " & _city)
            sb.AppendLine("Zip Code:  " & _zipCode)
            sb.AppendLine("Latitude:  " & Latitude_String)
            sb.AppendLine("Longitude:  " & Longitude_String)
            sb.AppendLine("Timezone:  " & _timeZone)
            sb.AppendLine("ISP:  " & _iSP)
            Return sb.ToString
        End Function
    
     

     Just as a simple example....

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim data As ExternalIP_Data = New ExternalIP_Data
            RichTextBox1.Text = data.ToString
        End Sub
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by BadNewsHughes Friday, June 16, 2017 1:49 PM
    Friday, June 9, 2017 12:49 AM

All replies

  • What doesn't work?  There should be no "conversion" necessary for versions of Visual Studio.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, June 7, 2017 9:34 PM
    Moderator
  • [Ip]-"This part works I just don't want to show you my IP"
    [City]-align="center"
    class="bord">


    Ben Hughes


    Wednesday, June 7, 2017 9:40 PM
  • thats what shows in the textbox

    Ben Hughes

    Wednesday, June 7, 2017 9:42 PM
  • thats what shows in the textbox

    If this is old code then it is likely that the web page has changed.  You need to insert a breakpoint after you get the page string and examine it in detail to confirm that the offsets etc that you use to get the city name out of that string are still correct, and adjust as required.

    Thursday, June 8, 2017 12:03 AM
  • Did you write this code in another version of Visual Studio and then try in VS2013 and not getting the proper results or was the code from someone else or from a book or webpage? Did the code ever work?

    Everything I see is not specific to any version of Visual Studio. What might make a difference (and not the case here) is the Framework version e.g. targeting an older version of the Framework.

    Of course the problem can be at the site you are downloading information from is a likely candidate.  Try building the request so it can run in your web browser e.g. Chrome, IE etc. does it work there??? If not then it's not code, otherwise it's code.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, June 8, 2017 12:54 AM
    Moderator
  • Ben,

    If you want the information then the following can help; if you want the info from that specific site, I won't help because that's all about "look at my Google ads" all over the place.

    There are better sites to get this info and one in particular is this one:

    http://ip-api.com/

    The API has a few formats that you can choose from and in the following, I've chosen to use XML, although JSON is smaller so you may want to consider that one.

    If you'll try the following in a new project, when the code gets to "Stop", hover your mouse over the variable named "data" and you'll see the information:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            Dim data As ExternalIP_Data = New ExternalIP_Data
    
            Stop
    
        End Sub
    
    
    End Class
    
    
    
    
    
    Public Class ExternalIP_Data
        Private _externalIP As System.Net.IPAddress
        Private _country As String
        Private _countryCode As String
        Private _region As String
        Private _regionName As String
        Private _city As String
        Private _zipCode As String
        Private _latitude As Double
        Private _longitude As Double
        Private _timeZone As String
        Private _iSP As String
    
        Public Sub New()
    
            Dim xDoc As XElement = XElement.Load("http://ip-api.com/xml")
    
            If xDoc...<status>.Value = "success" Then
                _country = xDoc...<country>.Value
                _countryCode = xDoc...<countryCode>.Value
                _region = xDoc...<region>.Value
                _regionName = xDoc...<regionName>.Value
                _city = xDoc...<city>.Value
                _zipCode = xDoc...<zip>.Value
                _latitude = CDbl(xDoc...<lat>.Value)
                _longitude = CDbl(xDoc...<lon>.Value)
                _timeZone = xDoc...<timezone>.Value
                _iSP = xDoc...<isp>.Value
                _externalIP = System.Net.IPAddress.Parse(xDoc...<query>.Value)
            End If
    
        End Sub
    
        Public ReadOnly Property City As String
            Get
                Return _city
            End Get
        End Property
    
        Public ReadOnly Property Country As String
            Get
                Return _country
            End Get
        End Property
    
        Public ReadOnly Property CountryCode As String
            Get
                Return _countryCode
            End Get
        End Property
    
        Public ReadOnly Property ExternalIP As System.Net.IPAddress
            Get
                Return _externalIP
            End Get
        End Property
    
        Public ReadOnly Property ExternalIP_String As String
            Get
                Return _externalIP.ToString
            End Get
        End Property
    
        Public ReadOnly Property ISP As String
            Get
                Return _iSP
            End Get
        End Property
    
        Public ReadOnly Property Latitude As Double
            Get
                Return _latitude
            End Get
        End Property
    
        Public ReadOnly Property Latitude_String As String
            Get
                Return _latitude.ToString("f4")
            End Get
        End Property
    
        Public ReadOnly Property Longitude As Double
            Get
                Return _longitude
            End Get
        End Property
    
        Public ReadOnly Property Longitude_String As String
            Get
                Return _longitude.ToString("f4")
            End Get
        End Property
    
        Public ReadOnly Property Region As String
            Get
                Return _region
            End Get
        End Property
    
        Public ReadOnly Property RegionName As String
            Get
                Return _regionName
            End Get
        End Property
    
        Public ReadOnly Property TimeZone As String
            Get
                Return _timeZone
            End Get
        End Property
    
        Public ReadOnly Property ZipCode As String
            Get
                Return _zipCode
            End Get
        End Property
    End Class


    I hope that helps.


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Proposed as answer by IronRazerz Friday, June 9, 2017 12:49 AM
    • Edited by Frank L. Smith Friday, June 9, 2017 9:45 AM ...modified code to correct an error
    • Marked as answer by BadNewsHughes Friday, June 16, 2017 1:49 PM
    Thursday, June 8, 2017 11:20 PM
  •  @ Frank,

     Nice work.  It works good.  Just on thing,  there is one line that needs to be corrected,  this line...

    _city = xDoc...<regionName>.Value
     

     Should be...

    _city = xDoc...<city>.Value
     

     Also,  just as a quick way to get all the info into a Label or TextBox/RichTextBox if anyone else,  or you,  want to copy/paste it into the class...

        Public Overrides Function ToString() As String
            Dim sb As New System.Text.StringBuilder
            sb.AppendLine("IP:  " & ExternalIP_String)
            sb.AppendLine("Country:  " & _country)
            sb.AppendLine("Country Code:  " & _countryCode)
            sb.AppendLine("Region:  " & _regionName)
            sb.AppendLine("Region Code:  " & _region)
            sb.AppendLine("City:  " & _city)
            sb.AppendLine("Zip Code:  " & _zipCode)
            sb.AppendLine("Latitude:  " & Latitude_String)
            sb.AppendLine("Longitude:  " & Longitude_String)
            sb.AppendLine("Timezone:  " & _timeZone)
            sb.AppendLine("ISP:  " & _iSP)
            Return sb.ToString
        End Function
    
     

     Just as a simple example....

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim data As ExternalIP_Data = New ExternalIP_Data
            RichTextBox1.Text = data.ToString
        End Sub
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by BadNewsHughes Friday, June 16, 2017 1:49 PM
    Friday, June 9, 2017 12:49 AM
  •  BadNewsHughes,

     I checked the page source of that site and apparently it has been changed since that code was written.  I got it working with some changes to the string parsing but,  doing it this way requires you to parse the actual source of the page and is not very reliable. The source can be changed at any time causing your app to crash or stop working.  I would recommend using Franks suggestion which appears that it should be a more reliable way than parsing the source string.  8)

     


    If you say it can`t be done then i`ll try it

    • Marked as answer by BadNewsHughes Friday, June 16, 2017 1:49 PM
    • Unmarked as answer by BadNewsHughes Friday, June 16, 2017 1:49 PM
    Friday, June 9, 2017 1:02 AM
  • IR,

    Thanks. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, June 9, 2017 9:46 AM