HttpClient调用过程中的超时异常TaskCanceledException

HttpClient调用远程Api过程中在默认超时时间内没有得到应答时报出异常TaskCanceledException

         var client = new HttpClient();
            client.Timeout = TimeSpan.FromMilliseconds(200);//设置超时时间
            try
            {
                var result = await client.GetStringAsync("/");
            }
            catch (Exception ex)
            {
                if (ex.InnerException is TaskCanceledException)
                {
                    LogWriter.ToWarn(ex);
                }
                else
                {
                    LogWriter.ToError(ex);
                }
            }

公司项目中有异常会通过发短信给开发人员。。。降低个级别。。防止短信轰炸。。。

var client = new HttpClient();
2client.Timeout = TimeSpan.FromMilliseconds(200);
3try
4{
5    var result = await client.GetStringAsync("http://blog.cincura.net/");
6}
7catch (HttpRequestException)
8{
9    // handle somehow
10    Console.WriteLine("HttpRequestException");
11}
12//catch (TimeoutException)
13//{
14// // handle somehow
15// Console.WriteLine("TimeoutException");
16//}
17catch (TaskCanceledException)
18{
19    // handle somehow
20    Console.WriteLine("TaskCanceledException");
21}

你可能感兴趣的:(HttpClient调用过程中的超时异常TaskCanceledException)