c# - 如果提供了无效域,则 HttpClient 或 WebClient 需要很长时间
问题描述
从无法解析的域中获取数据时,进入 catch 块需要超过 10 秒的时间。
try
{
var resultDomain = client.GetAsync("http://nonexistent.nonexistent.nonexistent").Result.Content.ReadAsStringAsync().Result;
}
catch(Exception ex)
{
//outputs
//The remote name could not be resolved: 'nonexistent.nonexistent.nonexistent'
//11,0632079
Console.WriteLine(ex.InnerException.InnerException.Message);
Console.WriteLine(watch.Elapsed.TotalSeconds);
}
这虽然命令
nslookup nonexistent.nonexistent.nonexistent
几乎立即完成域不存在的通知。有没有办法让 HttpClient/WebClient/... 的行为与 nslookup 一样快?.NET 还在等什么?
根据wireshark的说法,DNS会立即响应
解决方案
将超时设置为低于默认值 100 秒怎么样?
client.Timeout = TimeSpan.FromMilliseconds(500);
client.GetAsync(...);
推荐阅读
- python - 告诉 InlfuxDB 客户端(或任何 Python 请求客户端)信任自签名证书的正确方法?
- javascript - 如何从 Laravel 后端获取数据并在 Vue/Nuxt 前端显示
- node.js - NestJS:如何从同一个模块将服务注入提供者?
- c++ - 从函数中转换子类的问题
- python - 在 pyspark 中使用元组映射
- javascript - 为什么 HTML/React 页面出现语法错误
- r - Groupby 并根据模式计算 df 中元素的数量
- node.js - NtexJS/NodeJS 应用程序在构建时导致 TypeError: navigator.getUserMedia is not a function
- python - 在一个循环中,一旦“if”条件得到确保,它就会继续执行条件下方的语句,而不管条件。PYTHON
- reactjs - 如何使用反应钩子将父组件中声明的状态更新为子组件