Tuesday, June 30, 2009

Find GEO location through IP address

Geo IP Tool is a free service that helps you find geographical location via IP. I was trying to find out some service through which I can found the GEO information of customers via IP address. Below is a small helper code that can query Geo IP Tool against an IP. All you need is to place following helper class some where in your website. As you can see helper class only contains a single method GetLocationByIp of return type string. In fact it returns HTML containing returned information from Geo Ip Tool. Once you get the Geo information against an IP, all you need is to put it some literal control to be rendered.

Here is the GeoIPTool class, you can place it in your App_Code folder of ASP.NET website. Once you are done with this change then you can use user control code posted in next code portion for demo purpose.

using System;

using System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Net;
using
System.IO;

public static class GeoIPTool

{

public static string GetLocationByIP(string ipAddress)

{

string geoiptoolurl = "http://www.geoiptool.com/?IP={0}";

string gflag = "http://www.geoiptool.com/flags/";

string p1 = "<"+"table width=\"300\" height=\"300\"";

string p2 = " border=\"0\" cellpadding=\"4\"";

string p3 = " cellspacing=\"0\" class=\"tbl_style\">";

geoiptoolurl = string.Format(geoiptoolurl, ipAddress);

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(geoiptoolurl);

using (HttpWebResponse response = (HttpWebResponse)(request.GetResponse()))

{

using (StreamReader reader = new StreamReader(response.GetResponseStream()))

{

string htmlResult = reader.ReadToEnd();

if (!string.IsNullOrEmpty(htmlResult))

{

int sindex = htmlResult.IndexOf(p1+p2+p3);

htmlResult = htmlResult.Remove(0, sindex);

sindex = 0;

int eindex = htmlResult.IndexOf("");

string geoLocationTable = htmlResult.Substring(sindex, eindex + 8);

if (geoLocationTable.Contains("/flags/"))

geoLocationTable = geoLocationTable.Replace("/flags/",gflag);

return geoLocationTable;

}

}

}

return string.Empty;

}

}

Following is the demo user control, in order to use this code first you need to create a new user control file with name Sample.ascx in your website and then replace all its contents with following code. Finally replace Your IP Here text in GetLocationByIP call to your desired IP address.

<%@ Control Language="C#" ClassName="Sample" %>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)

{

string geoLocation = GeoIPTool.GetLocationByIP("YOUR IP HERE");

if (!String.IsNullOrEmpty(geoLocation))

GeoLocationHolder.Controls.Add(new LiteralControl(geoLocation));

}

<asp:PlaceHolder ID="GeoLocationHolder" runat="server">asp:PlaceHolder>

Please feel free provide your feedback!

No comments: