Trace Visitor GeoLocation in Asp.Net C#



In this post Trace Visitor GeoLocation in Asp.Net C#

Geolocation means recognizing or identifying the user’s location or any device’s location by the use of various data collection mechanisms. Most of the time these types of services use GPS devices or network routing addresses. Geolocation varies in support of various devices. Some browsers support geolocation while some don’t. So it can’t be said confidently that geolocation is always possible for a web application.

Signup on Website 

https://www.ipinfodb.com/register

Get Your API Key From Above Website After Registration

-----------------

Important Namespaces Used
--------------------------

using System.Net;
using System.Web.Script.Serialization;
using System.Data;
using System.Data.SqlClient;
using System.Activities;

---------------------------

Variable Defined
-----------------
static List<Location> locations = new List<Location>();
-----------------

Page Load Coding
-----------------

string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(ipAddress))
        {
            ipAddress = Request.ServerVariables["REMOTE_ADDR"];


        }
        locations.Clear();
        string APIKey = "YOUR API KEY";
        string url = string.Format("http://api.ipinfodb.com/v3/ip-city/?key={0}&ip={1}&format=json", APIKey, "64.68.200.46");
        using (WebClient client = new WebClient())
        {
            string json = client.DownloadString(url);
            Location location = new JavaScriptSerializer().Deserialize<Location>(json);
            locations.Add(location);
            GridView1.DataSource = locations;
            GridView1.DataBind();

        }
        if (Session["visitedagain"] == null)
        {
           
            SaveVisitorDetail();
            Session["visitedagain"] = "true";
        }

------------------

User Defined Method
--------------------

 public void SaveVisitorDetail()
    {
        string ipaddress = "" ;
        string countryname="";
        string cityname="";
        string statename="";
        foreach (var value in locations)
        {
            ipaddress=value.IPAddress;
            countryname = value.CountryName;
            cityname = value.CityName;
            statename = value.RegionName;
        }
        String query = "insert into visitordetails(visitdatetime,visitorip,country,city,state) values('" + DateTime.Now + "','" + ipaddress + "','" + countryname + "','" + cityname + "','" + statename + "')";
        String mycon = "Data Source=HP-PC\\SQLEXPRESS; Initial Catalog=VisitorGeoDetail; Integrated Security=true";
        SqlConnection con = new SqlConnection(mycon);
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = query;
        cmd.Connection = con;
        cmd.ExecuteNonQuery();
        
    }

-----------------

User Defined Class Declaration
-------------------------------

public class Location
{
    public string IPAddress { get; set; }
    public string CountryName { get; set; }
    public string CountryCode { get; set; }
    public string CityName { get; set; }
    public string RegionName { get; set; }
    public string ZipCode { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public string TimeZone { get; set; }
}

--------------------------------

0 Comment's

Comment Form