Display Visitor’s GeoLocation Using Web Service in VB.NET

Geolocation is a great way to put relevant content to your web visitors and it is a technology that can be used in almost any application you can think of.

In this tutorial, we use the IP2Location.io IP Geolocation API to lookup geolocation information from the visitor’s IP address. Instead of loading the full database, you can also lookup IP address via our API.

Below are the sample codes written in VB.NET to query and display visitor’s geolocation information.

Imports System.Net
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        WebAPI()
    End Sub

    Private Sub WebAPI()
        Dim strQuery As String
        Dim IPAddress As String = HttpContext.Current.Request.UserHostAddress
        Dim Key As String = "YOUR_API_KEY"
        Dim HttpWReq As HttpWebRequest
        Dim HttpWResp As Net.HttpWebResponse

        strQuery = "https://api.ip2location.io/?" & "ip=" & IPAddress & "&key=" & Key & "&format=json"
        HttpWReq = WebRequest.Create(strQuery)
        HttpWReq.Method = "GET"
        Try
            HttpWResp = HttpWReq.GetResponse()
            Dim reader As System.IO.StreamReader = New IO.StreamReader(HttpWResp.GetResponseStream())
            Dim content As String = reader.ReadToEnd
            Dim results As JObject = JsonConvert.DeserializeObject(content)
            Response.Write("ip: " & results("ip").ToString & "<br>" & vbNewLine)
            Response.Write("country_code: " & results("country_code").ToString & "<br>" & vbNewLine)
            Response.Write("country_name: " & results("country_name").ToString & "<br>" & vbNewLine)
            Response.Write("region_name: " & results("region_name").ToString & "<br>" & vbNewLine)
            Response.Write("city_name: " & results("city_name").ToString & "<br>" & vbNewLine)
            Response.Write("latitude: " & results("latitude").ToString & "<br>" & vbNewLine)
            Response.Write("longitude: " & results("longitude").ToString & "<br>" & vbNewLine)
            Response.Write("zip_code: " & results("zip_code").ToString & "<br>" & vbNewLine)
            Response.Write("time_zone: " & results("time_zone").ToString & "<br>" & vbNewLine)
            Response.Write("asn: " & results("asn").ToString & "<br>" & vbNewLine)
            Response.Write("as: " & results("as").ToString & "<br>" & vbNewLine)
            If results("isp") IsNot Nothing Then Response.Write("isp: " & results("isp").ToString & "<br>" & vbNewLine)
            If results("domain") IsNot Nothing Then Response.Write("domain: " & results("domain").ToString & "<br>" & vbNewLine)
            If results("net_speed") IsNot Nothing Then Response.Write("net_speed: " & results("net_speed").ToString & "<br>" & vbNewLine)
            If results("idd_code") IsNot Nothing Then Response.Write("idd_code: " & results("idd_code").ToString & "<br>" & vbNewLine)
            If results("area_code") IsNot Nothing Then Response.Write("area_code: " & results("area_code").ToString & "<br>" & vbNewLine)
            If results("weather_station_code") IsNot Nothing Then Response.Write("weather_station_code: " & results("weather_station_code").ToString & "<br>" & vbNewLine)
            If results("weather_station_name") IsNot Nothing Then Response.Write("weather_station_name: " & results("weather_station_name").ToString & "<br>" & vbNewLine)
            If results("mcc") IsNot Nothing Then Response.Write("mcc: " & results("mcc").ToString & "<br>" & vbNewLine)
            If results("mnc") IsNot Nothing Then Response.Write("mnc: " & results("mnc").ToString & "<br>" & vbNewLine)
            If results("mobile_brand") IsNot Nothing Then Response.Write("mobile_brand: " & results("mobile_brand").ToString & "<br>" & vbNewLine)
            If results("elevation") IsNot Nothing Then Response.Write("elevation: " & results("elevation").ToString & "<br>" & vbNewLine)
            If results("usage_type") IsNot Nothing Then Response.Write("usage_type: " & results("usage_type").ToString & "<br>" & vbNewLine)
            If results("address_type") IsNot Nothing Then Response.Write("address_type: " & results("address_type").ToString & "<br>" & vbNewLine)
            If results("district") IsNot Nothing Then Response.Write("district: " & results("district").ToString & "<br>" & vbNewLine)
            If results("ads_category") IsNot Nothing Then Response.Write("ads_category: " & results("ads_category").ToString & "<br>" & vbNewLine)
            If results("ads_category_name") IsNot Nothing Then Response.Write("ads_category_name: " & results("ads_category_name").ToString & "<br>" & vbNewLine)
            Response.Write("is_proxy: " & results("is_proxy").ToString & "<br>" & vbNewLine)

            If results("continent") IsNot Nothing Then
                Dim Continent = results("continent")
                Response.Write("continent => name: " & Continent("name").ToString & "<br>" & vbNewLine)
                Response.Write("continent => code: " & Continent("code").ToString & "<br>" & vbNewLine)
                Response.Write("continent => hemisphere: " & Continent("hemisphere").ToString & "<br>" & vbNewLine)
                Response.Write("continent => translation => lang: " & Continent("translation")("lang").ToString & "<br>" & vbNewLine)
                Response.Write("continent => translation => value: " & Continent("translation")("value").ToString & "<br>" & vbNewLine)
            End If

            If results("country") IsNot Nothing Then
                Dim Country = results("country")
                Response.Write("country => name: " & Country("name").ToString & "<br>" & vbNewLine)
                Response.Write("country => alpha3_code: " & Country("alpha3_code").ToString & "<br>" & vbNewLine)
                Response.Write("country => numeric_code: " & Country("numeric_code").ToString & "<br>" & vbNewLine)
                Response.Write("country => demonym: " & Country("demonym").ToString & "<br>" & vbNewLine)
                Response.Write("country => flag: " & Country("flag").ToString & "<br>" & vbNewLine)
                Response.Write("country => capital: " & Country("capital").ToString & "<br>" & vbNewLine)
                Response.Write("country => total_area: " & Country("total_area").ToString & "<br>" & vbNewLine)
                Response.Write("country => population: " & Country("population").ToString & "<br>" & vbNewLine)
                Response.Write("country => tld: " & Country("tld").ToString & "<br>" & vbNewLine)
                Response.Write("country => currency => code: " & Country("currency")("code").ToString & "<br>" & vbNewLine)
                Response.Write("country => currency => name: " & Country("currency")("name").ToString & "<br>" & vbNewLine)
                Response.Write("country => currency => symbol: " & Country("currency")("symbol").ToString & "<br>" & vbNewLine)
                Response.Write("country => language => code: " & Country("language")("code").ToString & "<br>" & vbNewLine)
                Response.Write("country => language => name: " & Country("language")("name").ToString & "<br>" & vbNewLine)
                Response.Write("country => translation => lang: " & Country("translation")("lang").ToString & "<br>" & vbNewLine)
                Response.Write("country => translation => value: " & Country("translation")("value").ToString & "<br>" & vbNewLine)
            End If

            If results("region") IsNot Nothing Then
                Dim Region = results("region")
                Response.Write("region => name: " & Region("name").ToString & "<br>" & vbNewLine)
                Response.Write("region => code: " & Region("code").ToString & "<br>" & vbNewLine)
                Response.Write("region => translation => lang: " & Region("translation")("lang").ToString & "<br>" & vbNewLine)
                Response.Write("region => translation => value: " & Region("translation")("value").ToString & "<br>" & vbNewLine)
            End If

            If results("city") IsNot Nothing Then
                Dim City = results("city")
                Response.Write("city => name: " & City("name").ToString & "<br>" & vbNewLine)
                Response.Write("city => translation => lang: " & City("translation")("lang").ToString & "<br>" & vbNewLine)
                Response.Write("city => translation => value: " & City("translation")("value").ToString & "<br>" & vbNewLine)
            End If

            If results("time_zone_info") IsNot Nothing Then
                Dim TimeZone = results("time_zone_info")
                Response.Write("time_zone_info => olson: " & TimeZone("olson").ToString & "<br>" & vbNewLine)
                Response.Write("time_zone_info => current_time: " & TimeZone("current_time").ToString & "<br>" & vbNewLine)
                Response.Write("time_zone_info => gmt_offset: " & TimeZone("gmt_offset").ToString & "<br>" & vbNewLine)
                Response.Write("time_zone_info => is_dst: " & TimeZone("is_dst").ToString & "<br>" & vbNewLine)
                Response.Write("time_zone_info => sunrise: " & TimeZone("sunrise").ToString & "<br>" & vbNewLine)
                Response.Write("time_zone_info => sunset: " & TimeZone("sunset").ToString & "<br>" & vbNewLine)
            End If

            If results("geotargeting") IsNot Nothing Then
                Dim GeoTarget = results("geotargeting")
                Response.Write("geotargeting => metro: " & GeoTarget("metro").ToString & "<br>" & vbNewLine)
            End If

            If results("proxy") IsNot Nothing Then
                Dim Proxy = results("proxy")
                Response.Write("proxy => last_seen: " & Proxy("last_seen").ToString & "<br>" & vbNewLine)
                Response.Write("proxy => proxy_type: " & Proxy("proxy_type").ToString & "<br>" & vbNewLine)
                Response.Write("proxy => threat: " & Proxy("threat").ToString & "<br>" & vbNewLine)
                Response.Write("proxy => provider: " & Proxy("provider").ToString & "<br>" & vbNewLine)
                Response.Write("proxy => is_vpn: " & Proxy("is_vpn").ToString & "<br>" & vbNewLine)
                Response.Write("proxy => is_tor: " & Proxy("is_tor").ToString & "<br>" & vbNewLine)
                Response.Write("proxy => is_data_center: " & Proxy("is_data_center").ToString & "<br>" & vbNewLine)
                Response.Write("proxy => is_public_proxy: " & Proxy("is_public_proxy").ToString & "<br>" & vbNewLine)
                Response.Write("proxy => is_web_proxy: " & Proxy("is_web_proxy").ToString & "<br>" & vbNewLine)
                Response.Write("proxy => is_web_crawler: " & Proxy("is_web_crawler").ToString & "<br>" & vbNewLine)
                Response.Write("proxy => is_residential_proxy: " & Proxy("is_residential_proxy").ToString & "<br>" & vbNewLine)
                Response.Write("proxy => is_spammer: " & Proxy("is_spammer").ToString & "<br>" & vbNewLine)
                Response.Write("proxy => is_scanner: " & Proxy("is_scanner").ToString & "<br>" & vbNewLine)
                Response.Write("proxy => is_botnet: " & Proxy("is_botnet").ToString & "<br>" & vbNewLine)
                Response.Write("proxy => is_consumer_privacy_network: " & Proxy("is_consumer_privacy_network").ToString & "<br>" & vbNewLine)
                Response.Write("proxy => is_enterprise_private_network: " & Proxy("is_enterprise_private_network").ToString & "<br>" & vbNewLine)
            End If
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub
End Class

Was this article helpful?

Related Articles