Search This Blog

Geolocation using ip address

Posted by Team on
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.

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****** 
        // :)
            String responseString;
            String[] responseArray = new String[20];
            responseArray[0] = "ERROR";
            if (ipaddress != "")
                var webClient = new WebClient();
                responseString = webClient.DownloadString(String.Format("" + ipaddress));
// OUTPUT                
// the result is "OK;;;IN;INDIA;MADHYA PRADESH;INDORE;-;22.7186;75.8558;+05:30"
                responseArray = responseString.Split(';');
                // or we can use'' , this will return a xml file and then go for xmlReader

            return responseArray;
        catch (Exception ex)
            throw ex;

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:
geolocation by ip address

While using ipinfodb api in C#, we have to convert the response in string through webclient

String responseString = webClient.DownloadString(String.Format("" + ipaddress));


Use'' , 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) :
For country precision (faster), simply do a query with the following API :

This is a sample of the output you will get in JSON with city precision :

 "statusCode" : "OK",
 "statusMessage" : "",
 "ipAddress" : "",
 "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 :
All the courtesy :