Thursday, July 2, 2009

How to get Client's Location using IPAddress

You can consume free Web Service, which can give IP Address Location Details.

http://www.aspsnippets.com/post/2009/04/12/Find-Visitors-Geographic-Location-using-IP-Address-in-ASPNet.aspx

The Following function returns Location Details as DataTable:

private DataTable GetLocation(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;

}

}

Thank You.

No comments: