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™ Web Service to lookup geolocation information from the visitor’s IP address. Instead of loading the full database, you can also lookup IP address via our hosted web service.

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

Protected Sub Page_Load(sender As Object, 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 serializer As New JavaScriptSerializer()
    Dim HttpWReq As HttpWebRequest
    Dim HttpWResp As Net.HttpWebResponse

    strQuery = "https://api.ip2location.com/v2/?" & "ip=" & IPAddress & "&key=" & Key & "&package=WS25&format=json"
    HttpWReq = WebRequest.Create(strQuery)
    HttpWReq.Method = "GET"
    HttpWResp = HttpWReq.GetResponse()

    Dim reader As System.IO.StreamReader = New IO.StreamReader(HttpWResp.GetResponseStream())
    Dim content As String = reader.ReadToEnd
    Dim item As Object = serializer.Deserialize(Of Object)(content)
    Dim country_code As String = item("country_code")
    Dim country_name As String = item("country_name")
    Dim region_name As String = item("region_name")
    Dim city_name As String = item("city_name")
    Dim latitude As String = item("latitude")
    Dim longitude As String = item("longitude")
    Dim zip_code As String = item("zip_code")
    Dim time_zone As String = item("time_zone")
    Dim isp As String = item("isp")
    Dim domain As String = item("domain")
    Dim net_speed As String = item("net_speed")
    Dim idd_code As String = item("idd_code")
    Dim area_code As String = item("area_code")
    Dim weather_station_code As String = item("weather_station_code")
    Dim weather_station_name As String = item("weather_station_name")
    Dim mcc As String = item("mcc")
    Dim mnc As String = item("mnc")
    Dim mobile_brand As String = item("mobile_brand")
    Dim elevation As String = item("elevation")
    Dim usage_type As String = item("usage_type")
    Dim address_type As String = item("address_type")
    Dim category As String = item("category")
    Dim category_name As String = item("category_name")

    Response.Write("country_code: " + country_code + "<br />")
    Response.Write("country_name: " + country_name + "<br />")
    Response.Write("region_name: " + region_name + "<br />")
    Response.Write("city_name: " + city_name + "<br />")
    Response.Write("latitude: " + latitude + "<br />")
    Response.Write("longitude: " + longitude + "<br />")
    Response.Write("zip_code: " + zip_code + "<br />")
    Response.Write("time_zone: " + time_zone + "<br />")
    Response.Write("isp: " + isp + "<br />")
    Response.Write("domain: " + domain + "<br />")
    Response.Write("net_speed: " + net_speed + "<br />")
    Response.Write("idd_code: " + idd_code + "<br />")
    Response.Write("area_code: " + area_code + "<br />")
    Response.Write("weather_station_code: " + weather_station_code + "<br />")
    Response.Write("weather_station_name: " + weather_station_name + "<br />")
    Response.Write("mcc: " + mcc + "<br />")
    Response.Write("mnc: " + mnc + "<br />")
    Response.Write("mobile_brand: " + mobile_brand + "<br />")
    Response.Write("elevation: " + elevation + "<br />")
    Response.Write("usage_type: " + usage_type + "<br />")
    Response.Write("address_type: " + address_type + "<br />")
    Response.Write("category: " + category + "<br />")
    Response.Write("category_name: " + category_name + "<br />")
End Sub

Sub Main()
    WebAPI()
End Sub

Was this article helpful?

Related Articles