Archive for November, 2010

Finding Geolocation information from ip address in

November 18, 2010 2 comments

Ip is the internet protocol for communication between nodes.

Ip is used to identify a host and address the location. If we need to identify the user

who are all accessing our website and store the ip address in our database is very simple and very easier.

This is the way to track the users ip address. Fetch a client’s ip address as soon as he access our web site in

Some of them may use a proxy ip address. But we can get their ip address with this simple code.

Add following namespace in page

using System.Net;

string ipAddress ="";

//Get the Host Name
string hostName = Dns.GetHostName();

//Get The Ip Host Entry
IPHostEntry ipHostEntry = Dns.GetHostEntry(hostName);

//Get The Ip Address From The Ip Host Entry Address List
IPAddress[] ipAddress = ipHostEntry.AddressList;

ipAddress = ipAddress[ipAddress.Length - 1].ToString();

To get the Geolocation of IP address you can use the various
API which would gives you result in various different format(csv,xml)

You can get Geolocation information from<your_api_key>&ip=

Response comes in XML format. using xml deserialize, I deserialize the response.

[XmlRootAttribute(ElementName = "Response", IsNullable = false)]

public class IPLocator
private string longitude;
public string Longitude
get { return longitude; }
set { longitude = value; }

private string latitude;
public string Latitude
get { return latitude; }
set { latitude = value; }
private string zip;
public string Zip
get { return zip; }
set { zip = value; }

private string ip;
public string IP
get { return ip; }
set { ip = value; }

After deserialization IPLocater class bind All properties of requested IP Address.

Binding Class is return IPLocater class.

Code of IPDetals Class

public IPLocator GetData(string ipAddress)
IPLocator ipLoc = new IPLocator();
//apiKey can be generated from below link


string apiKey = "anykey";
string path = "" + apiKey + "&ip=" + ipAddress + "&timezone=false";

WebClient client = new WebClient();
string[] eResult = client.DownloadString(path).ToString().Split(',');

if (eResult.Length > 0)
        ipLoc = (IPLocator)Deserialize(eResult[0].ToString());
{ }

return ipLoc;

//Desrialize XML String
private Object Deserialize(String pXmlizedString)
   XmlSerializer xs = new XmlSerializer(typeof(IPLocator));

   MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));

  XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

  return xs.Deserialize(memoryStream);


//String to UTF8ByteArray

private Byte[] StringToUTF8ByteArray(String pXmlString)

UTF8Encoding encoding = new UTF8Encoding();
Byte[] byteArray = encoding.GetBytes(pXmlString);
return byteArray;

//You can get the Geolocation infoamation here

string ipAddress = HttpContext.Current.Request.UserHostAddress;

IPDetails ipDetails=new IPDetails ();
IPLocator ipLocater = ipDetails.GetData(ipAddress);