http.dart HttpException:在收到完整的标头之前,连接已关闭

时间:2019-10-03 09:03:41

标签: api http flutter dart

通过http.dart在flutter上调用post API时,出现以下错误:

HttpException:在接收到完整标头之前,连接已关闭,uri = {http://192.168.3.218:12225/resourcabc/subresourceXYZ

为什么邮递员给出正确的答复。我也看到过有人问过这个问题。 另外,我正在物理设备上调试:三星

这是代码

  Future<Map> post(
String path, {
String token,
dynamic body,
bool parseResponse: false,
isFormData: false,
isUseBaseURL: false,
isEncoded: false,
  }) async {
Uri uri;
uri =
    isUseBaseURL ? Uri.parse('${Paths.baseUrl}/$path') : Uri.parse('$path');

print(uri.toString());
print(body.toString());

var request =
    await client.postUrl(uri).timeout(const Duration(seconds: 30));

if (token != null) {
  request.headers.add(HttpHeaders.authorizationHeader, 'Bearer $token');
} else {
  print('token is null');
  request.headers.add("X-Consumer-Custom-ID", "96");
}

if (body != null) {
  if (isFormData) {
    request
      ..headers.contentType = new ContentType(
          'application', 'x-www-form-urlencoded',
          charset: 'utf-8')
      ..write(body);
  } else {
    request
      ..headers.contentType = ContentType.json
      ..write(isEncoded ? body : json.encode(body));
  }
}
print(json.encode(body));
print('Sending data');
var response = await request.close();
print(response.statusCode);
Map responseMap = await _extractJson(response);
_checkAndThrowError(response, responseMap);
return responseMap;

}

1 个答案:

答案 0 :(得分:0)

只需用htttp替换https就可以解决问题 .replaceAll('https','http')

来源:https://github.com/flutter/flutter/issues/25107