need code to get the country of the user using IP Address using C#.net with any free api's.
Loading
need code to get the country of the user using IP Address using C#.net with any free api's.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Vishal YelveRank #178 · 13.6K reputationPosted Feb 16, 2023, 5:17 AM EST
https://rapidapi.com/category/Location
https://ipstack.com/
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 16, 2023, 6:53 AM EST
Are you asking for source code using the free API provided by a free API to get the country of the user based on their IP address, then below is the sample you can check:
using System; using System.Net; using System.IO; using Newtonsoft.Json; public class IPInfo { public string country { get; set; } // add more properties as needed } public static class IPInfoService { public static IPInfo GetIPInfo(string ipAddress) { string url = $"http://ip-api.com/json/{ipAddress}"; try { WebRequest request = WebRequest.Create(url); WebResponse response = request.GetResponse(); using (Stream stream = response.GetResponseStream()) { StreamReader reader = new StreamReader(stream); string responseJson = reader.ReadToEnd(); IPInfo ipInfo = JsonConvert.DeserializeObject(responseJson); return ipInfo; } } catch (WebException ex) { // handle exception return null; } } }You can call the GetIPInfo method by passing in the user's IP address as a string. The method will return an IPInfo object that contains various information about the IP address, including the country.
Note that the free version of IP-API has some usage limitations and rate limits. You should review the terms and conditions of the service to ensure that you comply with them. There are also many other IP geolocation APIs available that you can use instead if you prefer.
There are several IP geolocation APIs available, both free and paid. Here are some of the popular ones:
See below how you can use the IP Geolocation API to fetch the country name of a user w.r.t their IP address:
using System; using System.Net; using System.IO; using Newtonsoft.Json; public class IPInfo { public string CountryName { get; set; } // add more properties as needed } public static class IPInfoService { public static IPInfo GetIPInfo(string ipAddress) { string url = $"https://api.ipgeolocation.io/ipgeo?apiKey=YOUR_API_KEY&ip={ipAddress}"; try { WebRequest request = WebRequest.Create(url); WebResponse response = request.GetResponse(); using (Stream stream = response.GetResponseStream()) { StreamReader reader = new StreamReader(stream); string responseJson = reader.ReadToEnd(); IPInfo ipInfo = JsonConvert.DeserializeObject(responseJson); return ipInfo; } } catch (WebException ex) { // handle exception return null; } } }In this example, you need to replace YOUR_API_KEY with your actual API key that you get when you sign up for the IP Geolocation API service. The GetIPInfo method takes an IP address as a string and returns an IPInfo object that contains various information about the IP address, including the country.
You can use a similar approach to call other IP geolocation APIs as well. Note that the properties of the IPInfo class may vary depending on the API you use.
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 16, 2023, 4:00 AM EST
Hello Rakesh,
You can use blow api which is Free for non-commercial use.
https://ip-api.com/
Thanks
Naimish Makwana