CSharp · Console
using System;
using System.Net;
using Newtonsoft.Json;
public class GetGeoLocation
{
public IPData GetIPGeoLocation(string IP)
{
WebClient client = new WebClient();
// Make an api call and get response.
try
{
string response = client.DownloadString("http://ip-api.com/json/" + IP);
//Deserialize response JSON
IPData ipdata = JsonConvert.DeserializeObject<IPData>(response);
if (ipdata.status == "fail")
{
throw new Exception("Invalid IP");
}
return ipdata;
}
catch (Exception)
{
throw;
}
}
public static void Main()
{
IPData ipdata = new GetGeoLocation().GetIPGeoLocation("208.80.152.201");
Console.WriteLine(ipdata.status + " - Your IP belongs to '" + ipdata.region + " - " + ipdata.country + "'");
}
}
public class IPData
{
public string status { get; set; }
public string country { get; set; }
public string countryCode { get; set; }
public string region { get; set; }
public string regionName { get; set; }
public string city { get; set; }
public string zip { get; set; }
public string lat { get; set; }
public string lon { get; set; }
public string timezone { get; set; }
public string isp { get; set; }
public string org { get; set; }
public string @as { get; set; }
public string query { get; set; }
}