【发布时间】:2020-02-03 10:10:33
【问题描述】:
这是我的MultiPartRequest 代码
var request =
http.MultipartRequest("POST", Uri.parse(EMPLOYEE_PUNCH_IN_URL));
request.fields['uid'] = userId;
request.fields['location'] = location;
request.fields['punchin_time'] = punchInTime;
request.fields['punchin_location_name'] = address;
var multiPartFile = await http.MultipartFile.fromPath(
"photo", imageFile.path,
contentType: MediaType("image", "$extension"));
request.files.add(multiPartFile);
http.StreamedResponse response = await request.send();
var responseByteArray = await response.stream.toBytes();
employeePunchInModel = standardSerializers.deserializeWith(
EmployeePunchInModel.serializer,
json.decode(utf8.decode(responseByteArray)));
......
我知道如何将超时设置为正常的 http 请求。我已经关注了这个链接
Set timeout for HTTPClient get() request
我尝试通过以下方式添加超时功能,但它不起作用并且我的请求已完成
1.
var multiPartFile = await http.MultipartFile.fromPath(
"photo", imageFile.path,
contentType: MediaType("image", "$extension")).timeout(const Duration(seconds: 1));
2.
http.StreamedResponse response = await request.send().timeout(const Duration(seconds: 1));
3.
var responseByteArray = await response.stream.toBytes().timeout(const Duration(seconds: 15));
但上述超时都不起作用。
【问题讨论】:
-
选项 1 设置从文件构建多部分请求的超时。所以如果例如。它是一个需要很长时间才能读取的大文件或设备 IO 很慢,它会在构建请求之前超时。选项 2 设置接收响应标头的超时。这通常意味着服务器已经接收并处理了整个请求,并且已经响应了一个状态码。选项 3 设置接收其余响应的超时时间 - 响应正文。你到底想设置什么超时??
-
@Ovidiu 我想为发送到服务器的 http 请求设置超时
-
那是选项 2。如果服务器在给定时间内没有接收、读取和处理整个请求,然后回复返回给应用程序的 HTTP 状态代码,则将被视为超时。
-
这是关于 C# 的吗?
-
@Momoro 关于 Dart 语言和 Flutter 框架