La classe HttpClient in Windows.Web.Http espone una serie di metodi per integrarsi al meglio con le API asincrone di WinRT.
Ad esempio, il metodo GetAsync restituisce un IAsyncOperationWithProgress
Tra gli overload del metodo possiamo passare come parametro un oggetto di tipo CancellationToken con cui possiamo controllare il task asincrono. Il CancellationToken è esposto come proprietà di CancellationTokenSource oltre a vari metodi, ad esempio con CancelAfter possiamo impostare il tempo in cui il task sarà cancellato dal momento della sua creazione.
var cts = new CancellationTokenSource();
cts.CancelAfter(2000);
var client = new HttpClient();
var response = await client.GetAsync(new Uri("https://www.winrtitalia.com")).AsTask(cts.Token);
if (!response.IsSuccessStatusCode)
{
//messaggio
return;
}
var txt = await response.Content.ReadAsStringAsync();Nell'esempio, la chiamata HTTP sarà annullata se la sua risposta non arriverà entro il tempo impostato come timeout nel metodo CancelAfter.
Commenti
Per inserire un commento, devi avere un account.
Fai il login e torna a questa pagina, oppure registrati alla nostra community.
Approfondimenti
Response streaming con Blazor e .NET 10
Pubblicare un MCP Server in GitHub MCP Registry
Effettuare un clone parziale di un repository di GitHub
Creare una cache temporanea in JavaScript
Escludere alcuni file da GitHub Copilot
Breaking the Legacy Barrier: how to Use AI to Modernize Applications
Definire il colore di una scrollbar HTML tramite CSS
Esporre un server MCP con Azure API Management
Importare repository da Bitbucket a GitHub Enterprise Cloud
Ottimizzare gli indici con Automatic Index Compaction in Azure SQL Database
Gestire pubblicazione Kubernetes tramite .NET Aspire
Utilizzare i named query filter di Entity Framework


