You need to await the GetAsync
method. It will then throw a TaskCanceledException
if it has timed out. Additionally, GetStringAsync
and GetStreamAsync
internally handle timeout, so they will NEVER throw.
string baseAddress = "http://localhost:8080/";
var client = new HttpClient()
{
BaseAddress = new Uri(baseAddress),
Timeout = TimeSpan.FromMilliseconds(1)
};
try
{
var s = await client.GetAsync();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.InnerException.Message);
}