Here is a method for C-sharp lovers, It will fetch the IP Address of Client and some other browser info of the host when you run it after compiling your code.
We are just creating Http Request and an object of HttpBrowserCapabilities class. Now with this object we can collect all the host specific data that we will send to service to get the Geolocation of the host.
This geolocation can be written in other server side languages like PHP/JAVA etc, using the same service. Developer can decide at the time that he/she wants a text or xml response from the service.
The xml response using above code would look like this:
While using ipinfodb api in C#, we have to convert the response in string through webclient
OR
Use http://api.hostip.info/?ip='' , this will return a xml file and then go for xmlReader
FOR Javascript/ JSON lovers here is the code:
IP Address Geolocation JSON API The API returns the location of an IP address (country, region, city, zipcode, latitude and longitude) and the associated timezone in JSON format. Usage
For city precision, simply do a query at this address (if you omit the IP parameter it will check with your own IP) : http://api.ipinfodb.com/v3/ip-city/?key=&ip=74.125.45.100&format=json
For country precision (faster), simply do a query with the following API : http://api.ipinfodb.com/v3/ip-country/?key=&ip=74.125.45.100&format=json
Output
This is a sample of the output you will get in JSON with city precision :
for API KEY and more documentation links:
Registration : http://ipinfodb.com/register.php
Description: http://ipinfodb.com/ip_location_api.php
All the courtesy : http://ipinfodb.com/
We are just creating Http Request and an object of HttpBrowserCapabilities class. Now with this object we can collect all the host specific data that we will send to service to get the Geolocation of the host.
private string UserLogEntry(string flag) { String publicIpAddress = HttpContext.Current.Request.UserHostAddress.ToString(); HttpBrowserCapabilities oBrowserCap = Request.Browser; String browserName = oBrowserCap.Browser.ToString(); String version = oBrowserCap.Version.ToString(); String majorVersion =oBrowserCap.MajorVersion.ToString(); String minorVersion =oBrowserCap.MinorVersion.ToString(); String platform = oBrowserCap.Platform.ToString(); String isBeta = oBrowserCap.Beta.ToString(); String isCrawler =oBrowserCap.Crawler.ToString(); String isAOL = oBrowserCap.AOL.ToString(); String location = ""; String ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
String[] responseArray = GetLocation(publicIpAddress); if (responseArray[0].Equals("ERROR")) { //there is an error in response from service } else if (responseArray[0].Equals("OK")) { location = responseArray[6] + '-' + responseArray[5] + '-' + responseArray[4]; //[0]-successFlag [1]-nahi pata, [2]-ip, [3]-country abbrivieted, [4]-country fullname, [5]-State, [6]-city, [7]-zip, [8]-lat, [9]-long, [10]- pata nahi } string msg = (string)SqlHelper.ExecuteScalar(Config.XcatLinkAccountDB, "spInsertUpdateLog", Convert.ToInt32(Session["LoginUserId"]), Convert.ToString(Session.SessionID), publicIpAddress, DBNull.Value, location, browserName + ' ' + version, DBNull.Value, DBNull.Value, flag); return msg; }
// then here is a method that is used in above method to get the location of that IP Address
private String[] GetLocation(string ipaddress) { // here is the API Key: 2000cf96f94f92711724080f77d60246ff9e5eaac83d14076a1fb090346e3a8c // User Id : s******.acs@gmail.com // :) try { String responseString; String[] responseArray = new String[20]; responseArray[0] = "ERROR"; if (ipaddress != "127.0.0.1") { var webClient = new WebClient(); responseString = webClient.DownloadString(String.Format("http://api.ipinfodb.com/v3/ip-city/?key=2000cf96f94f92711724080f77d60246ff9e5eaac83d14076a1fb090346e3a8c&ip=" + ipaddress)); // OUTPUT
// the result is "OK;;183.182.87.10;IN;INDIA;MADHYA PRADESH;INDORE;-;22.7186;75.8558;+05:30" responseArray = responseString.Split(';'); // or we can use http://api.hostip.info/?ip='' , this will return a xml file and then go for xmlReader
} return responseArray; } catch (Exception ex) { throw ex; //Response.Write(ex.Message); } }
This geolocation can be written in other server side languages like PHP/JAVA etc, using the same service. Developer can decide at the time that he/she wants a text or xml response from the service.
The xml response using above code would look like this:
While using ipinfodb api in C#, we have to convert the response in string through webclient
String responseString = webClient.DownloadString(String.Format("http://api.ipinfodb.com/v3/ip-city/?key=2000cf96f94f92711724080f77d60246ff9e5eaac83d14076a1fb090346e3a8c&ip=" + ipaddress));
OR
Use http://api.hostip.info/?ip='' , this will return a xml file and then go for xmlReader
FOR Javascript/ JSON lovers here is the code:
IP Address Geolocation JSON API The API returns the location of an IP address (country, region, city, zipcode, latitude and longitude) and the associated timezone in JSON format. Usage
For city precision, simply do a query at this address (if you omit the IP parameter it will check with your own IP) : http://api.ipinfodb.com/v3/ip-city/?key=
For country precision (faster), simply do a query with the following API : http://api.ipinfodb.com/v3/ip-country/?key=
Output
This is a sample of the output you will get in JSON with city precision :
{ "statusCode" : "OK", "statusMessage" : "", "ipAddress" : "74.125.45.100", "countryCode" : "US", "countryName" : "UNITED STATES", "regionName" : "CALIFORNIA", "cityName" : "MOUNTAIN VIEW", "zipCode" : "94043", "latitude" : "37.3956", "longitude" : "-122.076", "timeZone" : "-08:00" }
for API KEY and more documentation links:
Registration : http://ipinfodb.com/register.php
Description: http://ipinfodb.com/ip_location_api.php
All the courtesy : http://ipinfodb.com/