Track Client Location From IP Address in ASP.NET


Introduction

In last article Find Client IP And Location we seen how to find client IP address and location using http://freegeoip.appspot.com/  which is not free webservice to get the client location information.
 
In this article we will discuss second method to find client location on the basis of Ip Address of the client.

BackGround:

In so many cases web developer's need to track client location information eg. You are developing the commercial website and you need to show the user what kind of offer's are available but you don't know which area he belongs to. In such cases instead to asking user which area you belongs and then show the offer's in that area we can track his location and without asking his location information can show the details.

In last article I've used the freegeoip.appspot.com by passing the IP address of client and then get the location here also same thing we will do but with different service provider i.e. http://api.ipinfodb.com . For accessing the service from ipinfodb you need to register first they will provide you a API key which we will use in our application to make request for location details. So just register HERE. And get the API key.

GetLocation:

The GetLocation method will accept IP address to track location and retuns a DataTable which contains location details of the IP address.

private DataTable GetLocation(string ipaddress)
    {
        string url = "http://api.ipinfodb.com/v2/ip_query.php?key={0}&ip={1}&timezone=true";

        url = String.Format(url, "<your API key Here", ipaddress);

        HttpWebRequest httpWRequest = (HttpWebRequest)WebRequest.Create(url);
        using (HttpWebResponse httpWResponse = (HttpWebResponse)httpWRequest.GetResponse())
        {

            XmlTextReader xtr = new XmlTextReader(httpWResponse.GetResponseStream());
            DataSet ds = new DataSet();
            ds.ReadXml(xtr);
            return ds.Tables[0];
        }

    }

In above GetLocation method instead of <your API key> write the API key provided by apinfodb.com after registration and then call the method which will return the location details of user.

Conclusion:

By using this simple API we can track our client very easily.


Similar Articles