【问题标题】:http get on dart take long time to return status of the responsehttp get on dart 需要很长时间才能返回响应状态
【发布时间】:2020-01-29 17:11:31
【问题描述】:

我正在使用这个方法来检查应用是否可以访问目标url

Future<bool> ip() async {
var url = 'http://192.168.2.176/applications.html';try {
http.Response response = await http.get((url));return true;} catch (_) {print('WTF');
return false;}}

我的问题是,如果响应失败,函数将返回 false 那么它需要大约 30 秒才能将布尔结果作为假值给我。 有什么办法可以解决这个问题, 如果 url 不可访问,我需要该函数在 5 秒内返回 false。

【问题讨论】:

标签: http flutter dart


【解决方案1】:

看看这段代码可能对你有帮助:

try {
  final request = await client.get(...);
  final response = await request.close()
    .timeout(const Duration(seconds: 2));
  // rest of the code
  ...
} on TimeoutException catch (_) {
  // A timeout occurred.
} on SocketException catch (_) {
  // Other exception
}

相应地修改你的代码让我知道它是否有效。

【讨论】:

    猜你喜欢
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多