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 user’s geolocation information.

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
End Sub
Private Sub WebAPI()
    Dim strQuery As String
    Dim IPAddress As String = HttpContext.Current.Request.UserHostAddress
    Dim Key As String = "demo"
    Dim serializer As New JavaScriptSerializer()
    Dim HttpWReq As HttpWebRequest
    Dim HttpWResp As Net.HttpWebResponse
    strQuery = "http://api.ip2location.com/?" & "ip=" & IPAddress & "&key=" & Key & "&package=WS24&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 city As String = item("city_name")
    Dim countryc As String = item("country_code")
    Dim countryn As String = item("country_name")
    Dim region As String = item("region_name")
    Dim lat As String = item("latitude")
    Dim longi As String = item("longitude")
    Dim timez As String = item("time_zone")
    Dim zip As String = item("zip_code")
    Response.Write("Welcome Visitors from " + city + "<br />")
    Response.Write("Your IP address: " + IPAddress + "<br />")
    Response.Write("Country Code: " + countryc + "<br />")
    Response.Write("Country Name: " + countryn + "<br />")
    Response.Write("Region Name: " + region + "<br />")
    Response.Write("Latitude: " + lat + "<br />")
    Response.Write("Longitude: " + longi + "<br />")
    Response.Write("Time Zone: " + timez + "<br />")
    Response.Write("Zip Code: " + zip)
End Sub
Sub Main()
End Sub


Was this article helpful?

Related Articles