Tuesday 4 October 2011

Get IP to Country ,State , city info in asp.net ,C#



First register on this site http://api.ip2locationapi.com/ to get API Key

protected void GetUserLocation()
{
try
{
ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipaddress == null)
{
ipaddress = Request.ServerVariables["REMOTE_ADDR"];
}
DataTable dt = UserManage.GetLocationnew(ipaddress);
if(dt.Rows.Count > 0)
{
Location = dt.Rows[0]["countryName"].ToString();
CountryID = Common.GetCountryID(Location);
StateID = Common.GetStateID(dt.Rows[0]["region"].ToString());
}
}
catch (Exception ex)
{
AppBase.handleException("Form:changevote, Function:GetUserLocation", ex);
}

}

public static DataTableGetLocationnew(string ipaddress)
{
DataSet ds = new DataSet();
try
{
DataTable dt = new DataTable();
WebRequest wreq = WebRequest.Create("http://api.ip2locationapi.com/?user= &format=xml&ip=" + ipaddress + "");
WebResponse wres = wreq.GetResponse();
XmlTextReader xmlRead = new XmlTextReader(wres.GetResponseStream());
ds.ReadXml(xmlRead);
}
catch (Exception ex)
{
AppBase.handleException("Form:_Default, Function:GetLocation", ex);
}
returnds.Tables[0];
}


Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger