我在测试代码中写了这段代码,但是抛出的One or more errors occurred. (A task was canceled.)
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient()
{
BaseAddress = new Uri("https://www.taobao.com"),
Timeout = TimeSpan.FromMilliseconds(10)
};
Assert.Throws<TimeoutException>(() =>
{
var s = client.GetAsync("").Result;
});
Assert.Throws() Failure
Expected: typeof(System.TimeoutException)
Actual: typeof(System.AggregateException): One or more errors occurred. (A task was canceled.)
大家在代码中都是如何捕获这个错误的呢?
Github上的讨论:
HttpClient throws TaskCanceledException on timeout
StackOverflow上的讨论:
How can I tell when HttpClient has timed out?
相关问题
- Timeout in authentication does not work properly
- Limiting the timeout period for Selenium FindEleme
- MS Captcha timeout duration in registration form i
- bash read timeout only for the first character ent
- Timeout for Z3 Optimize
相关文章
- httpclient导致线程池爆了
- How exactly do Firebase Analytics handle session d
- Response body is null, status is 200
- Body parameters cannot be used with form parameter
- rxjs timeout to first value
- Should I cache and reuse HttpClient created from H
- Android error : MultipartEntity , request sent by
- how do i set a timeout value for python's mech
HttpClient.Timeout 属性的帮助文档:
Timeout 设置只是的是一个等待时间,
Timeout = TimeSpan.FromMilliseconds(10)
只是表示等10秒,如果超过 10 秒就取消当前的 Task ,实际 TCP 层面没有发生超时。在这样的情况下,对于 HttpClient 来说就是我等了10秒钟你没来,我走人,我根本不知道你为什么没来,你让 HttpClient 报什么更有用的异常?