gpt4 book ai didi

http - Dart:http.dart 包中的 client.post() 方法挂起并且没有返回 future

转载 作者:行者123 更新时间:2023-12-03 03:58:54 24 4
gpt4 key购买 nike

我目前正在 dart 中实现一个简单的 ONVIF 客户端程序,并且在处理 http.dart 包中的 future 时遇到了一些麻烦。

在我下面的代码中,client.post()方法返回 Future<response> (包含正文/标题/状态代码等...),在我的情况下,我需要在 if/else 语句之前收到它,因此我使用了 await .问题是程序只是挂起并且没有继续通过 client.post()线。
我知道我可能需要做一个 client.close()某处,但我尝试了很多不同的方法,但没有任何效果。这是我当前的代码,并附有一些注释来尝试解释一下:

// The Variables: 
// reqSysDateAndTime is a soap message we are sending to the device.
// onvifDev is just a place where the device details are stored.
// probeMatch is a class that stores important info from the ws-discovery stage.

Future<String> checkXaddrsAndGetTime(Device onvifDev, ProbeMatch probeMatch) async {

// Set up the client and uri.
Uri uri = Uri.parse(probeMatch.xaddrs);
http.Client client = http.Client();

// Send the POST request, with full SOAP envelope as the request body
print('[Setup]: Listening for Date/Time response...');
Response response = await client.post(uri, body: reqSysDateAndTime);
print("${response.body}");

// Determine if the address is usable or not.
if (response != null) {
// Set this address as 'working'
onvifDev.xAddrs = probeMatch.xaddrs;
return response.body;
}
else {
return null; // The address does not work
}
}

我也知道这不是请求的实际正文的问题,因为如果我这样做......
client.post(uri, body: reqSysDateAndTime).then((onValue) => print(onValue.body));

...相反,它会打印出我期望的响应。

我知道这可能是我缺少的一个小修复,但任何帮助将不胜感激。

干杯。

最佳答案

为了简要回答我自己的问题,结果证明这是一个愚蠢的错误,我使用的地址是链接本地地址 - 因此它为什么卡在 client.post() . InternetAddress 类中有一个不错的方法here ,在其他一些有用的方法中,它检查地址是否是链接本地的。

关于http - Dart:http.dart 包中的 client.post() 方法挂起并且没有返回 future ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58804162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com