Click here to Skip to main content
12,824,299 members (43,409 online)
Click here to Skip to main content
Add your own
alternative version


115 bookmarked
Posted 2 Mar 2010

Easy to Track the Geographical Location Based on IP Address

, 2 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Easy to track the geographical location based on IP Address

Table of Contents


Developers are very much familiar with the use of IP tracking system, Microsoft Visual Studio .NET provides a number of class, methods to do this. This article is not about getting the user IP only, but also finds the geographical location of a user who is browsing your ASP.NET application. For example, you have an ASP.NET application, your hosting is done, your web address is suppose ””, now you want to track / maintain a log of the visitors IP with the location something like:


Sample output figure:

Quick Overview

Before we start, we need to know some basic knowledge, on System.Net, System.Data namespace provide by Microsoft Visual Studio .Net, HTTP Server variables.

More information can be found at this link.

How to Achieve

If you search for the solution on the internet; you may get many ways to do it. For example, you can use web service or download database containing the location mapped with the IP, but most of them are not free to use / allow you to a very limited number of hits per day… I found some sites that allow you free access for getting the user location from IP, some of the site(s) are listed below:

Note: All the above listed addresses reply in standard XML format.

How to Use the Services

In this section, I would like to discuss how to use the site(s) to retrieve a user geographical location. You can choose any one of them, before that you need to know what are the parameters required, let's start one by one:


Parameter: IP Address (
URL sample:
Output: Standard XML

<?xml version="1.0" encoding="UTF-8"?>
  • Parameter: IP Address ( & License Key
  • URL sample:
  • Output: Standard XML
<?xml version="1.0" encoding="utf-8"?>
<IPInformation xmlns:xsi= 
	xmlns:xsd="" xmlns="">
  <Organization />
  <TimeZone />
  <RegionName />

Blocks of code should be set as style "Formatted" like this:

  • Parameter: IP Address (
  • URL sample:
  • Output: Standard XML
<?xml version="1.0" encoding="UTF-8"?>

Get the User IP

I use a very common technique. Actually this is nothing but the using of HTTP server variables. The following server variables are used for this purpose.


A sample code snippet is given below:

private string GetVisitor()
        string strIPAddress = string.Empty;
        string strVisitorCountry = string.Empty;

        strIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (strIPAddress == "" || strIPAddress == null)
            strIPAddress = Request.ServerVariables["REMOTE_ADDR"];

        Tools.GetLocation.IVisitorsGeographicalLocation _objLocation;
        _objLocation = new Tools.GetLocation.ClsVisitorsGeographicalLocation();

        DataTable _objDataTable = _objLocation.GetLocation(strIPAddress);

        if (_objDataTable != null)
            if (_objDataTable.Rows.Count > 0)
                strVisitorCountry = 
                            "IP: "
                            + strIPAddress
                            + ", TIMESTAMP: " 
                            + Convert.ToString(System.DateTime.Now)     
                            + ", CITY: "
                            + Convert.ToString(_objDataTable.Rows[0]["City"]).ToUpper()
                            + ", COUNTRY: "
                            + Convert.ToString(_objDataTable.Rows[0]
                            + ", COUNTRY CODE: "
                            + Convert.ToString(_objDataTable.Rows[0]
                strVisitorCountry = null;
        return strVisitorCountry;

Get the User Location

To get the location, you just need to use the following provided by Microsoft Visual Studio .NET:

  • WebRequest
  • WebResponse
  • WebProxy

More information can be found at this link.

A sample code snippet is given below:

public DataTable GetLocation(string strIPAddress)
            //Create a WebRequest with the current Ip
            WebRequest _objWebRequest =
                               + strIPAddress);
            //Create a Web Proxy
            WebProxy _objWebProxy =
               new WebProxy(""
                         + strIPAddress, true);

            //Assign the proxy to the WebRequest
            _objWebRequest.Proxy = _objWebProxy;

            //Set the timeout in Seconds for the WebRequest
            _objWebRequest.Timeout = 2000;

                //Get the WebResponse 
                WebResponse _objWebResponse = _objWebRequest.GetResponse();
                //Read the Response in a XMLTextReader
                XmlTextReader _objXmlTextReader
                    = new XmlTextReader(_objWebResponse.GetResponseStream());

                //Create a new DataSet
                DataSet _objDataSet = new DataSet();
                //Read the Response into the DataSet

                return _objDataSet.Tables[0];
                return null;
        } // End of GetLocation		 


I hope this might be helpful to you! Enjoy.


  • MSDN


  • 2nd March, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Md. Marufuzzaman
Bangladesh Bangladesh
A well experienced leader with successful track record of software development, product innovations, brand management and corporate communication etc. Some successful product innovations have also achieved and awards “Most Valuable Professional” (MVP) at 2010 and 2011 by and also selected as a mentor of Published over 100 technical articles in various software development resource sites (i.e.,, Microsoft MSDN, and IEEE & IBM (In progress)) and various IT Forums, Blogs etc.

Over ten years of professional experiences in ICT field having extensive experience in formulating corporate vision and long term strategy. Leading development related functions including design, development, services, data management and analytics, customer experience management, content services, digital analytics and optimization.

An individual with results-driven approach and relentless in pursuit of excellence from a business and organizational standpoint. Believes in transparency, commitment and teamwork.

Expertise: Software/Solution Architect, SaaS platform base application, Large scale win32/web based business software solutions, enterprise applications, integration, etc.

Technologies/Tools: Microsoft.Net version 05/08/10/12, Microsoft SQL Server version 7/2K/05/08/12 , Oracle version 10/11, MySql version 5.1, 5.5, PS2, Visual C#, R, VB.NET, ASP.NET, PHP, API, MVC, WebAPI , Add-In Visual Basic etc.,. I have also more than two years’ of strong experience in mobile-VAS (platform development).

Points of Interest: Technology and research & development especially focused on business functionalities and social business areas as well, few stuff including:

1.R&D on new techniques as required to increase business revenue.
2.Urban and rural sectors to improve people’s lifestyle, better medical facilities, education, social business etc., using innovative technical solutions.
3.Research and innovative product development.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
tanweer akhtar1-Jul-12 19:54
membertanweer akhtar1-Jul-12 19:54 
GeneralRe: My vote of 5 Pin
Md. Marufuzzaman10-Sep-12 20:44
mentorMd. Marufuzzaman10-Sep-12 20:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170308.1 | Last Updated 2 Mar 2010
Article Copyright 2010 by Md. Marufuzzaman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid