I am working on .net core 3.1. I am trying to use https://services.timeanddate.com/ API.
when debuggeing I got IsCancellationRequested = true
and go to exception. The request that was canceled that line below:
using var response = await _client.GetAsync(url);
service
public class HolidayService : IHolidayService
{
private readonly HttpClient _client;
private readonly IHttpClientFactory _clientFactory;
static string httpaddr = "https://api.xmltime.com"; // I need this url result in post man "https://api.xmltime.com/holidays?accesskey=dYjG8ztthf&secretkey=veUV06dNmrnp7bbaYq0u&version=3&country=ro&year=2021"
// static string httpaddr = "https://date.nager.at"; // for this I can get result in postman "https://date.nager.at/api/v3/publicholidays/2020/US"
static HttpClient Http = new HttpClient() { BaseAddress = new Uri(httpaddr) };
public HolidayService(HttpClient client)
{
_client = client;
_client.Timeout = TimeSpan.FromMilliseconds(30);
}
public HolidayService(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
public async Task<List<Holiday>> GetHolidays(string country, int year)
{
string url = string.Format($"/holidays?accesskey=dYjG8ztthf&secretkey=veUV06dNmrnp7bbaYq0u&version=3&country=ro&year=2021&lang=en");
// for url i get IsCancellationRequested = true and go to TaskCanceledException exception . How to solve?
// string url2 = string.Format($"/api/v2/PublicHolidays/2020/US"); // for this ulr2 there is no exception
var result = new List<Holiday>();
try
{
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
var response = await Http.GetAsync(url, token);
if (response.IsSuccessStatusCode)
{
using var responseStream = await response.Content.ReadAsStreamAsync();
result = await JsonSerializer.DeserializeAsync<List<Holiday>>(responseStream);
}
else
{
throw new HttpRequestException(response.ReasonPhrase);
}
// var response = await _client.GetAsync(url, token);
// using var response = await _client.GetAsync(url);
}
// Filter by InnerException.
catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException)
{
// Handle timeout.
Console.WriteLine("Timed out: " + ex.Message);
}
catch (TaskCanceledException ex)
{
// Handle cancellation.
Console.WriteLine("Canceled: " + ex.Message); // this is my exception
}
return result;
}
I want to know why this cancelation exception is occurs? For this https://date.nager.at my code is working fine and got json results as excepted. But Why https://services.timeanddate.com/ API give exception and request was canceled that line below:
using var response = await _client.GetAsync(url);
Why i could not get excepted result from this https://services.timeanddate.com/ API.
JSON result is for https://services.timeanddate.com/ API.:
{
"urlid": null,
"url": null,
"country": null,
"name": null,
"oneliner": null,
"date": null,
"types": null,
"uid": null
}
How to get all response from this https://services.timeanddate.com/ API?