Anda di halaman 1dari 2

It is very common questions for new web developer how to get the clients country l ocation to track the

user.In various forum I have faced this questions. So that today I want to share how to get clients count ry location,longitude,latitude,country code etc. To get this you need to use a free database which has IP to Location mapping or call a web service that does the same for you. Using free web services is the easy way to get the location of the user based on its IP address. After goggling I have found the following web service which provide this service absolutely free and that too wi thout any complex interface to do the same. http://freegeoip.appspot.com/ The above website provides free IP Geolocation Web Service that returns data in three formats . 1. XML [Extended Markup Language] 2. CSV [Comma Separated Values] 3. JSON [JavaScript Object Notation] Here I am explaining how to get the data in XML format. It is very easy to use this web service,just send your ip address through the UR L like: http://freegeoip.appspot.com/xml/116.68.204.254 The returned XML with result are as below: <Response> <Status>true</Status> <Ip>116.68.204.254</Ip> <CountryCode>BD</CountryCode> <CountryName>Bangladesh</CountryName> <RegionCode>81</RegionCode> <RegionName>Dhaka</RegionName> <City>Dhaka</City> <ZipCode/> <Latitude>23.723</Latitude> <Longitude>90.4086</Longitude> </Response> How to consume this web service and get the result: Here WebRequest and WebProxy is responsible for make call this url and xml respo nse is received by WebResponse and store it to dataset using XMLTextReader. Code: private DataTable GetGeoLocation(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; } } How to show result in aspx page: You can place this code on Page_Load event or under any button event whenever yo u like: Protected void ShowGeoLocatio() { string sIpaddress; sIpaddress= Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if(sIpaddress== sIpaddress== null) { sIpaddress= Request.ServerVariables["REMOTE_ADDR"]; } //call the function to consume web service and store result into datatable DataTable odtGeoLocation = GetGeoLocation(sIpaddress); if (odtGeoLocation != null) { if (odtGeoLocation .Rows.Count > 0) { lblCity.Text = odtGeoLocation .Rows[0]["City"].ToString(); lblRegion.Text = odtGeoLocation .Rows[0]["RegionName"].ToString(); lblCountry.Text = odtGeoLocation .Rows[0]["CountryName"].ToString(); lblCountryCode.Text = odtGeoLocation .Rows[0]["CountryCode"].ToString(); } else { lblError=Sorry,no data found!!; } } } Hope that it may helps the developer.Thanks.

Anda mungkin juga menyukai