Geo Location .net core
public async Task<Geolocation> GetLocation(Geolocation Geolocation)
{
var ip = HttpContext.Connection.RemoteIpAddress.ToString();
/*string url = string.Format(
"http://pro.ip-api.com/json/{0}?key=xgLWzHqvscq2aG3", ip);*/
string url = "http://pro.ip-api.com/json/103.86.158.154?key=xgLWzHqvscq2aG3";
using (var client = new WebClient())
{
try
{
string response = client.DownloadString(url);
var json = JObject.Parse(response);
Geolocation geolocation = new Geolocation();
geolocation.CountryCode = json.Get("countryCode");
geolocation.Country = json.Get("country");
geolocation.Region = json.Get("region");
geolocation.RegionName = json.Get("regionName");
geolocation.City = json.Get("city");
geolocation.ISP = json.Get("isp");
geolocation.Zip = json.Get("zip");
geolocation.IpAddress = ip?.ToString();
geolocation.CreateDate = DateTime.Now;
return geolocation;
}
catch(Exception e)
{
throw e;
}
}
}
Tidak ada komentar