TechieClues TechieClues
Updated date Jun 16, 2022
How To Get Free Geolocation From IP Address using C#
  • 17.9k
  • 0
  • 0

In this blog, we will see how to get geolocation details on a given IP address using Free Api in C#. In a real-world scenario, we may need geolocation details from the user's IP address details for your website. 

IP API - HTTP GET Request

IP API is free and easy to use and integrate with your C# applications. You have to just send HTTP GET request along with the IP address to the below API URL to get geolocation information.

http://ip-api.com/json/206.189.139.240

JSON result from IP API:

{
    "status": "success",
    "country": "India",
    "countryCode": "IN",
    "region": "KA",
    "regionName": "Karnataka",
    "city": "Bengaluru",
    "zip": "560100",
    "lat": 12.9634,
    "lon": 77.5855,
    "timezone": "Asia/Kolkata",
    "isp": "DigitalOcean, LLC",
    "org": "Digital Ocean",
    "as": "AS14061 DigitalOcean, LLC",
    "query": "206.189.139.240"
}

IP Stack - HTTP GET Request

IP Stack API is also free and easy to use with your C# applications. In order to use IP Stack API, you need to pass the API key with the API address. It's an easy process, you can sign up with an IP stack and get your free API key. 

Once you received your API Key, You have to just send HTTP GET request along with the IP address and API key to the below API URL to get geolocation information for the user.

http://api.ipstack.com/199.187.211.62?access_key=<YOUR API KEY>

JSON result from IP Stack API:

{
    "ip": "199.187.211.62",
    "type": "ipv4",
    "continent_code": "NA",
    "continent_name": "North America",
    "country_code": "US",
    "country_name": "United States",
    "region_code": "WA",
    "region_name": "Washington",
    "city": "Seattle",
    "zip": "98168",
    "latitude": 47.490108489990234,
    "longitude": -122.2915267944336,
    "location": {
        "geoname_id": 5809844,
        "capital": "Washington D.C.",
        "languages": [
            {
                "code": "en",
                "name": "English",
                "native": "English"
            }
        ],
        "country_flag": "http://assets.ipstack.com/flags/us.svg",
        "country_flag_emoji": "🇺🇸",
        "country_flag_emoji_unicode": "U+1F1FA U+1F1F8",
        "calling_code": "1",
        "is_eu": false
    }
}

Get geolocation information Using IP API in C#

The below code shows how to retrieve geolocation information from the given IP address using IP API.

using System;
using System.Net.Http;
namespace GetGeoLocation
{
    class Program
    {
        static void Main(string[] args)
        {
            // IP API URL
            var Ip_Api_Url = "http://ip-api.com/json/206.189.139.232"; // 206.189.139.232 - This is a sample IP address. You can pass yours if you want to test          

            // Use HttpClient to get the details from the Json response
            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.DefaultRequestHeaders.Accept.Clear();
                httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));                
                // Pass API address to get the Geolocation details 
                httpClient.BaseAddress = new Uri(Ip_Api_Url);
                HttpResponseMessage httpResponse = httpClient.GetAsync(Ip_Api_Url).GetAwaiter().GetResult();
                // If API is success and receive the response, then get the location details
                if (httpResponse.IsSuccessStatusCode)
                {
                    var geolocationInfo = httpResponse.Content.ReadAsAsync<LocationDetails_IpApi>().GetAwaiter().GetResult();
                    if (geolocationInfo != null)
                    {
                        Console.WriteLine("Country: " + geolocationInfo.country);
                        Console.WriteLine("Region: " + geolocationInfo.regionName);
                        Console.WriteLine("City: " + geolocationInfo.city);
                        Console.WriteLine("Zip: " + geolocationInfo.zip);
                        Console.ReadKey();
                    }
                }
            }          
        }
    }  

    public class LocationDetails_IpApi
    {
        public string query { 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 region { get; set; }
        public string regionName { get; set; }
        public string status { get; set; }
        public string timezone { get; set; }
        public string zip { get; set; }
    }
}

Output:

Country: India
Region: Karnataka
City: Bengaluru
Zip: 560100

Get geolocation information Using IP Stack API in C#

The below code shows how to get the geolocation details from the given IP address using IP Stack API.

using System;
using System.Net.Http;

namespace GetGeoLocation
{
    class Program
    {
        static void Main(string[] args)
        {            
            var Ip_Stack_Url = "http://api.ipstack.com/199.188.211.50?access_key=<YOUR API KEY>";  // 199.188.211.50 - This is a sample IP address. You can pass yours if you want to test

            // Use HttpClient to get the details from the Json response
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                // Pass API address to get the Geolocation details 
                client.BaseAddress = new Uri(Ip_Stack_Url);
                HttpResponseMessage response = client.GetAsync(Ip_Stack_Url).GetAwaiter().GetResult();
                // If API is success and receive the response, then get the location details
                if (response.IsSuccessStatusCode)
                {
                    var geolocationInfo = response.Content.ReadAsAsync<LocationDetails_IpStack>().GetAwaiter().GetResult();
                    if (geolocationInfo != null)
                    {
                        Console.WriteLine("Country: " + geolocationInfo.country_name);
                        Console.WriteLine("Region: " + geolocationInfo.region_name);
                        Console.WriteLine("City: " + geolocationInfo.city);
                        Console.WriteLine("Zip: " + geolocationInfo.zip);
                        Console.ReadKey();
                    }
                }
            }
        }
    }

    public class LocationDetails_IpStack
    {
        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 { get; set; }        
        public double latitude { get; set; }
        public double longitude { get; set; }        
    }
}

Output:

Country: United States
Region: Missouri
City: Clayton
Zip: 63101

 

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!