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 ”www.xyz.com”, now you want to track / maintain a log of the visitors IP with the location something like:
IP: XXX.XXX.XXX.XXX, TIMESTAMP: 3/2/2010 4:18:39 PM, COUNTRY= BANGLADESH,
COUNTRY CODE= BD, CITY= DHAKA, etc.
Sample output figure:

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.
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.
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:
(i)http://freegeoip.appspot.com
Parameter: IP Address (xxx.xxx.xxx.xxx).
URL sample: http://freegeoip.appspot.com/xml/xxx.xxx.xxx.xxx
Output: Standard XML
="1.0" ="UTF-8"
<Response>
<Status>true</Status>
<Ip>xxx.xxx.xxx.xxx</Ip>
<CountryCode>BD</CountryCode>
<CountryName>Bangladesh</CountryName>
<RegionCode>81</RegionCode>
<RegionName>Dhaka</RegionName>
<City>Dhaka</City>
<ZipCode></ZipCode>
<Latitude>23.723</Latitude>
<Longitude>90.4086</Longitude>
</Response>
(ii)http://ws.cdyne.com/
- Parameter: IP Address (xxx.xxx.xxx.xxx) & License Key
- URL sample: http://ws.cdyne.com/ip2geo/ip2geo.asmx/ResolveIP?ipAddress=xxx.xxx.xxx.xxx&licenseKey=0
- Output: Standard XML
="1.0" ="utf-8"
<IPInformation xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ws.cdyne.com/">
<City>Dhaka</City>
<StateProvince>81</StateProvince>
<Country>Bangladesh</Country>
<Organization />
<Latitude>23.72301</Latitude>
<Longitude>90.4086</Longitude>
<AreaCode>0</AreaCode>
<TimeZone />
<HasDaylightSavings>false</HasDaylightSavings>
<Certainty>90</Certainty>
<RegionName />
<CountryCode>BD</CountryCode>
</IPInformation>
Blocks of code should be set as style "Formatted" like this:
(iii)http://ipinfodb.com/
- Parameter: IP Address (xxx.xxx.xxx.xxx)
- URL sample:http://ipinfodb.com/ip_query.php?ip=xxx.xxx.xxx.xxx0
- Output: Standard XML
="1.0" ="UTF-8"
<Response>
<Ip>xxx.xxx.xxx.xxx</Ip>
<Status>OK</Status>
<CountryCode>BD</CountryCode>
<CountryName>Bangladesh</CountryName>
<RegionCode>81</RegionCode>
<RegionName>Dhaka</RegionName>
<City>Dhaka</City>
<ZipPostalCode></ZipPostalCode>
<Latitude>23.7231</Latitude>
<Longitude>90.4086</Longitude>
<Timezone>6</Timezone>
<Gmtoffset>6</Gmtoffset>
<Dstoffset>6</Dstoffset>
</Response>
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.
HTTP_X_FORWARDED_FOR
REMOTE_ADDR
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]
["CountryName"]).ToUpper()
+ ", COUNTRY CODE: "
+ Convert.ToString(_objDataTable.Rows[0]
["CountryCode"]).ToUpper();
}
else
{
strVisitorCountry = null;
}
}
return strVisitorCountry;
}
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)
{
WebRequest _objWebRequest =
WebRequest.Create(http: + strIPAddress);
WebProxy _objWebProxy =
new WebProxy("http://freegeoip.appspot.com/xml/"
+ strIPAddress, true);
_objWebRequest.Proxy = _objWebProxy;
_objWebRequest.Timeout = 2000;
try
{
WebResponse _objWebResponse = _objWebRequest.GetResponse();
XmlTextReader _objXmlTextReader
= new XmlTextReader(_objWebResponse.GetResponseStream());
DataSet _objDataSet = new DataSet();
_objDataSet.ReadXml(_objXmlTextReader);
return _objDataSet.Tables[0];
}
catch
{
return null;
}
}
I hope this might be helpful to you! Enjoy.
- 2nd March, 2010: Initial post
He is the founder & CEO of MNH Technologies and working for urban and rural sectors to improve people’s lifestyle, better medical facilities, education, social business etc. He has over ten years of professional experiences in design and developing Client-Server, Multi-Tier, Database, Web based business software solutions, Enterprise Applications, API, WebAPI, Google Analytics implementation, Add-In, Documentation & Technical Writing etc for Windows / Mac using Microsoft SQL Server, Oracle, MySql, PS, C#, VB.NET, ASP.NET, PHP, RoR, Visual Basic etc. He has also more than two years experience in Mobile-VAS (Platform Development).
He worked for various software development & technology consulting. His core focus on technologies to create dynamic data-driven systems that add value to your business and dynamic technology consulting that builds advanced solutions for the industries across the various vertices.
He also work as a Solution Architect at Dhrupadi Techno Consortium Limited (DTCL) and responsible for analyzing business requirements and offered optimum solutions (multiple options), which would address all current requirements, provide flexibility for future growth and allow smooth transition between old system and new system.
He graduated with honors from The University of Asia Pacific, in Computer Science and Engineering. He was awarded as “Most Valuable Professional” (MVP) at 2010 and 2011 by CodeProject.com and also selected as a Mentor of CodeProject.com
Specialties: Software Development Management, System Integration, Data Warehouse Architecture, Virtualization.