在这段生成的客户端从生产端点读取价格前,对它进行审查。
读取按固定区域性表示的 decimal;正文无效时返回 null;保留传输失败;遵守取消;避免泄露 URL 中的秘密;并复用连接。
csharp
using System;
using System.Globalization;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
public sealed class PriceClient
{
public async Task<decimal?> ReadAsync(string url, CancellationToken token)
{
try
{
using var client = new HttpClient();
string text = await client.GetStringAsync(url, token);
return decimal.Parse(text);
}
catch (Exception error)
{
Console.WriteLine($"failed {url}: {error}");
throw error;
}
}
}
生成代码仅作示例,不代表任何特定模型