Hi RichardSa,
Your class properties name are worng. As per the api returns, the properties should be like below.
public class Location
{
public string ip { get; set; }
public string country_code { get; set; }
public string country_name { get; set; }
public string region_name { get; set; }
public string city_name { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public string zip_code { get; set; }
public string time_zone { get; set; }
public string asn { get; set; }
public string @as { get; set; }
public bool is_proxy { get; set; }
}
Refer below code.
HTML
<asp:Label ID="Timelbl" runat="server" />
Namespaces
C#
using System.Linq;
using System.Net;
using System.Web.Script.Serialization;
VB.Net
Imports System.Linq
Imports System.Net
Imports System.Web.Script.Serialization
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
LoginDateTime();
}
private Location GetLocation()
{
string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(ipAddress))
{
ipAddress = Request.ServerVariables["REMOTE_ADDR"];
}
//string ipAddress = "209.142.68.29";
string APIKey = "API_Key";
string url = string.Format("https://api.ip2location.io/?key={0}&ip={1}&format=json", APIKey, ipAddress);
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
using (WebClient client = new WebClient())
{
string json = client.DownloadString(url);
Location location = new JavaScriptSerializer().Deserialize<Location>(json);
return location;
}
}
private void LoginDateTime()
{
DateTime time1 = Convert.ToDateTime("2023/06/19 22:25");
TimeZoneInfo serverTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Alaskan Standard Time");
TimeZoneInfo userTimeZone = TimeZoneInfo.FindSystemTimeZoneById(GetTimeZoneNameByOffsetTime(this.GetLocation().time_zone.Replace("+", "")));
DateTime userLocalTime = TimeZoneInfo.ConvertTime(time1, serverTimeZone, userTimeZone);
Timelbl.Text = userLocalTime.ToString("dddd, MMMM d, yyyy h:mm tt");
}
public string GetTimeZoneNameByOffsetTime(string offset)
{
return TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(x => x.BaseUtcOffset == TimeSpan.Parse(offset)).StandardName;
}
public class Location
{
public string ip { get; set; }
public string country_code { get; set; }
public string country_name { get; set; }
public string region_name { get; set; }
public string city_name { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public string zip_code { get; set; }
public string time_zone { get; set; }
public string asn { get; set; }
public string @as { get; set; }
public bool is_proxy { get; set; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
LoginDateTime()
End Sub
Private Function GetLocation() As Location
Dim ipAddress As String = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If String.IsNullOrEmpty(ipAddress) Then
ipAddress = Request.ServerVariables("REMOTE_ADDR")
End If
Dim APIKey As String = "API_Key"
Dim url As String = String.Format("https://api.ip2location.io/?key={0}&ip={1}&format=json", APIKey, ipAddress)
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
Using client As WebClient = New WebClient()
Dim json As String = client.DownloadString(url)
Dim location As Location = New JavaScriptSerializer().Deserialize(Of Location)(json)
Return location
End Using
End Function
Private Sub LoginDateTime()
Dim time1 As DateTime = Convert.ToDateTime("2023/06/19 22:25")
Dim serverTimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Alaskan Standard Time")
Dim userTimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(GetTimeZoneNameByOffsetTime(Me.GetLocation().time_zone.Replace("+", "")))
Dim userLocalTime As DateTime = TimeZoneInfo.ConvertTime(time1, serverTimeZone, userTimeZone)
Timelbl.Text = userLocalTime.ToString("dddd, MMMM d, yyyy h:mm tt")
End Sub
Public Function GetTimeZoneNameByOffsetTime(ByVal offset As String) As String
Return TimeZoneInfo.GetSystemTimeZones().FirstOrDefault(Function(x) x.BaseUtcOffset = TimeSpan.Parse(offset)).StandardName
End Function
Public Class Location
Public Property ip As String
Public Property country_code As String
Public Property country_name As String
Public Property region_name As String
Public Property city_name As String
Public Property latitude As Double
Public Property longitude As Double
Public Property zip_code As String
Public Property time_zone As String
Public Property asn As String
Public Property [as] As String
Public Property is_proxy As Boolean
End Class
Output
Tuesday, June 20, 2023 1:25 AM