Web Api Blog

 
 
 

There are 2 free api which we preferred for getting IP to Location details.

They are:

  • FreeGeoIP - Max request limit - 15,000 requests per hour
  • IP-API - Max request limit - 150 requests per minute

Both the api's are having equally good uptime, however we found FreeGeoIP more reliable by comparing sample set of IP with their locations.

Download code from github


FreeGeoIP

Consuming FreeGeoIP using Postman:

Web Api Url: http://freegeoip.net/json/116.73.154.132

Api Response:

{
    "ip": "116.73.154.132",
    "country_code": "IN",
    "country_name": "India",
    "region_code": "MH",
    "region_name": "Maharashtra",
    "city": "Mumbai",
    "zip_code": "400020",
    "time_zone": "Asia/Kolkata",
    "latitude": 18.975,
    "longitude": 72.8258,
    "metro_code": 0
}

IP-Api

Consuming IP-Api using Postman:

Web Api Url: http://ip-api.com/json/67.163.30.159

Api Response:

{
    "as": "AS7922 Comcast Cable Communications, LLC",
    "city": "Downers Grove",
    "country": "United States",
    "countryCode": "US",
    "isp": "Comcast Cable",
    "lat": 41.811,
    "lon": -88.0302,
    "org": "Comcast Cable",
    "query": "67.163.30.159",
    "region": "IL",
    "regionName": "Illinois",
    "status": "success",
    "timezone": "America/Chicago",
    "zip": "60515"
}

C# Code for IP to Location


Step 1:

Create a console application


Step 2:

Add following nuget packages

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net45" />
</packages>

Step 3:

Create a C# POCO class for storing Json Response.

public class IP2Location_IP_API
{
    public string @as { get; set; }
    public string city { get; set; }
    public string country { get; set; }
    public string countryCode { get; set; }
    public string isp { get; set; }
    public double lat { get; set; }
    public double lon { get; set; }
    public string org { get; set; }
    public string query { get; set; }
    public string region { get; set; }
    public string regionName { get; set; }
    public string status { get; set; }
    public string timezone { get; set; }
    public string zip { get; set; }
}

Step 4:

Write Web API Consumer for Get Request

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace IP2Location
{
    class Program
    {
        static void Main(string[] args)
        {
            var API_PATH_IP_API = "http://ip-api.com/json/14.99.228.232";
            var API_PATH_Free_Geo_IP = "http://freegeoip.net/json/116.73.154.132";


            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                //For IP-API
                client.BaseAddress = new Uri(API_PATH_IP_API);
                HttpResponseMessage response = client.GetAsync(API_PATH_IP_API).GetAwaiter().GetResult();
                if (response.IsSuccessStatusCode)
                {
                    var locationDetails = response.Content.ReadAsAsync<IP2Location_IP_API>().GetAwaiter().GetResult();
                    if (locationDetails != null)
                    {
                        Console.WriteLine("Country: " + locationDetails.country);
                        Console.WriteLine("Region: " + locationDetails.regionName);
                        Console.WriteLine("City: " + locationDetails.city);
                    }
                }
            }


            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                //For Free-GEO-IP API
                client.BaseAddress = new Uri(API_PATH_Free_Geo_IP);
                HttpResponseMessage response= client.GetAsync(API_PATH_Free_Geo_IP).GetAwaiter().GetResult();
                if (response.IsSuccessStatusCode)
                {
                    var locationDetails = response.Content.ReadAsAsync<IP2Location_FreeGeoIP>().GetAwaiter().GetResult();
                    if (locationDetails != null)
                    {
                        Console.WriteLine("Country: " + locationDetails.country_name);
                        Console.WriteLine("Region: " + locationDetails.region_name);
                        Console.WriteLine("City: " + locationDetails.city);
                    }
                }
            }
        }
    }


    public class IP2Location_FreeGeoIP
    {
        public string ip { get; set; }
        public string country_code { get; set; }
        public string country_name { get; set; }
        public string region_code { get; set; }
        public string region_name { get; set; }
        public string city { get; set; }
        public string zip_code { get; set; }
        public string time_zone { get; set; }
        public double latitude { get; set; }
        public double longitude { get; set; }
        public int metro_code { get; set; }
    }

    public class IP2Location_IP_API
    {
        public string @as { get; set; }
        public string city { get; set; }
        public string country { get; set; }
        public string countryCode { get; set; }
        public string isp { get; set; }
        public double lat { get; set; }
        public double lon { get; set; }
        public string org { get; set; }
        public string query { get; set; }
        public string region { get; set; }
        public string regionName { get; set; }
        public string status { get; set; }
        public string timezone { get; set; }
        public string zip { get; set; }
    }
}


 
 
Didn't find what you were looking for? Find more on Free IP to Location Api - C#