Dart:http.dart 包中的 client.post() 方法挂起并且不返回未来
Dart: client.post() method in the http.dart package is hanging and isn't returning a future
我目前正在 dart 中实现一个简单的 ONVIF 客户端程序,在 http.dart 包中处理期货时遇到了一些麻烦。
在我下面的代码中,client.post()
方法 returns a Future<response>
(包含 body/header/status 代码等等......)在我的例子中我需要在 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));
...相反,它会打印出我期待的响应。
我知道这可能是我遗漏的一个小修复,但我们将不胜感激任何帮助。
干杯。
为了简要回答我自己的问题,事实证明这是我的一个愚蠢错误,我使用的地址是 link-local - 因此它挂在 client.post()
。在 InternetAddress class here 中有一个很好的方法,以及其他一些有用的方法,它检查地址是否是 link-本地地址。
我目前正在 dart 中实现一个简单的 ONVIF 客户端程序,在 http.dart 包中处理期货时遇到了一些麻烦。
在我下面的代码中,client.post()
方法 returns a Future<response>
(包含 body/header/status 代码等等......)在我的例子中我需要在 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));
...相反,它会打印出我期待的响应。
我知道这可能是我遗漏的一个小修复,但我们将不胜感激任何帮助。
干杯。
为了简要回答我自己的问题,事实证明这是我的一个愚蠢错误,我使用的地址是 link-local - 因此它挂在 client.post()
。在 InternetAddress class here 中有一个很好的方法,以及其他一些有用的方法,它检查地址是否是 link-本地地址。