我目前正在用dart实现一个简单的ONVIF客户端程序,并且在处理http.dart包中的期货时遇到了一些麻烦。
在下面的代码中,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));
...相反,它将打印出我期望的响应。
我知道这可能是我所缺少的一个小修正,但是任何帮助将不胜感激。
干杯。