Sunday, June 28, 2009

Geographic information of the User on your page!!!

Wanted to show the geographical information of the visitor on homepage of your site. Here is a great API to your rescue
http://ipinfodb.com/ip_query2.php?ip=(YourMachinesIP)
I have written this sample code for using this API for your reference have a look :
protected void Page_Load(object sender, System.EventArgs e)
{
string userip;
string url;
userip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (userip== null)
{
userip = Request.ServerVariables["REMOTE_ADDR"];
}
url = String.Format("
http://ipinfodb.com/ip_query2.php?ip=%7B0%7D", userip);
WebClient wc = new WebClient();
Stream data = wc.OpenRead(url);
String str;
using (StreamReader sr = new StreamReader(data))
{
str = sr.ReadToEnd();
data.Close();
}
//Use data is str variable to display on web page….
}

Happy coding!!!

Vinod

Happy coding!!!!!

No comments: