Wednesday 7 March 2012

Get Location Info from an IP address

First of all,you need to get an API key to use this service. You will get it free from http://ipinfodb.com/
After generating your API key,you need to call a service url with the IP (that you want to look up). The Response by the requested service returns in XML format.
Here is a simple running code.


 private DataTable GetLocation(string ipaddress)
    {
        DataTable dt = new DataTable();
        WebRequest wreq = WebRequest.Create("http://api.ipinfodb.com/v2/ip_query.php?key=<your key goes here>&timezone=true&ip=" + ipaddress);
        WebProxy proxy = new WebProxy("http://api.ipinfodb.com/v2/ip_query.php?key=<your key goes here>&timezone=true&ip=" + ipaddress, true);


        wreq.Proxy = proxy;
        wreq.Timeout = 5000 ;
        WebResponse wres = wreq.GetResponse();
        XmlTextReader xmlRead = new XmlTextReader(wres.GetResponseStream());
        DataSet ds = new DataSet();
        ds.ReadXml(xmlRead);
        return ds.Tables[0];


    }


To call above method,

 DataTable dt = GetLocation("117.194.12.179");


Enjoy.

No comments:

Post a Comment