const string M_URL_TIME_API = "http://worldtimeapi.org/api/timezone/Asia/Ho_Chi_Minh";
public async Task<TimeData> GetTimeDataAsync()
{
JsonSerializerOptions serializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
};
try
{
HttpClient client = new HttpClient();
Uri uri = new Uri(string.Format(M_URL_TIME_API, string.Empty));
HttpResponseMessage response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
var timeData = JsonSerializer.Deserialize<TimeData>(content, serializerOptions);
return timeData;
}
}
catch
{
}
return new TimeData {
datetime = System.DateTime.Now
};
}