Pages

Thursday, December 29, 2011

How to get visitor's Country,longitude,latitude from IP address is asp.net

It is very common questions for new web developer how to get the client's country location to track the user.In various forum I

have faced this questions. So that today I want to share how to get client's country location,longitude,latitude,country code etc.
To get this you need to use a

free database which has IP to Location mapping
or
call a web service that does the same for you.

Using free web services is the easy way to get the location of the user based on its IP address. After goggling I have found the

following web service which provide this service absolutely free and that too without any complex interface to do the same.

http://freegeoip.appspot.com/

The above website provides free IP Geolocation Web Service that returns data in three formats .

1. XML [Extended Markup Language]
2. CSV [Comma Separated Values]
3. JSON [JavaScript Object Notation]

Here I am explaining how to get the data in XML format.
It is very easy to use this web service,just send your ip address through the URL
like:
http://freegeoip.appspot.com/xml/116.68.204.254

The returned XML with result are as below:

<Response>
<Status>trueStatus>
<Ip>116.68.204.254Ip>
<CountryCode>BDCountryCode>
<CountryName>BangladeshCountryName>
<RegionCode>81RegionCode>
<RegionName>DhakaRegionName>
<City>DhakaCity>
<ZipCode/>
<Latitude>23.723Latitude>
<Longitude>90.4086Longitude>
Response>

How to consume this web service and get the result:
Here WebRequest and WebProxy is responsible for make call this url and xml response is received by WebResponse and store it to dataset using XMLTextReader.

Code:
private DataTable GetGeoLocation(string ipaddress)
{
//Create a WebRequest
WebRequest rssReq = WebRequest.Create("http://freegeoip.appspot.com/xml/"
+ ipaddress);
//Create a Proxy
WebProxy px =new WebProxy("http://freegeoip.appspot.com/xml/" + ipaddress, true);

//Assign the proxy to the WebRequest
rssReq.Proxy = px;

//Set the timeout in Seconds for the WebRequest
rssReq.Timeout = 2000;
try
{ //Get the WebResponse
WebResponse rep = rssReq.GetResponse();

//Read the Response in a XMLTextReader
XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream());

//Create a new DataSet
DataSet ds = new DataSet();

//Read the Response into the DataSet
ds.ReadXml(xtr);
return ds.Tables[0];
}
catch
{
return null;
}
}

How to show result in aspx page:
You can place this code on Page_Load event or under any button event whenever you like:
Protected void ShowGeoLocatio()
{
string sIpaddress;
sIpaddress= Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if(sIpaddress== "" || sIpaddress== null)
{
sIpaddress= Request.ServerVariables["REMOTE_ADDR"];
}

//call the function to consume web service and store result into datatable
DataTable odtGeoLocation = GetGeoLocation(sIpaddress);
if (odtGeoLocation != null)
{
if (odtGeoLocation .Rows.Count > 0)
{
lblCity.Text = odtGeoLocation .Rows[0]["City"].ToString();
lblRegion.Text = odtGeoLocation .Rows[0]["RegionName"].ToString();
lblCountry.Text = odtGeoLocation .Rows[0]["CountryName"].ToString();
lblCountryCode.Text = odtGeoLocation .Rows[0]["CountryCode"].ToString();
}
else
{
lblError="Sorry,no data found!!";
}
}
}

No comments: