本文介绍了http.dart HttpException:在收到完整标头之前连接已关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

while calling a post API on flutter via http.dart i am getting following error:

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

当邮递员给出正确答复时为什么会这样.我之前也看到有人问过这个问题.另外,我正在物理设备上调试:samsung

Why is it so when postman is giving correct response. I have seen this issue being asked before too.Also, i am debugging on a physical device: samsung

这是代码

  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;

}

推荐答案

只需将 https 替换为 htttp 即可解决问题.replaceAll('https', 'http')

just replace https with htttp solved the problem.replaceAll('https', 'http')

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

这篇关于http.dart HttpException:在收到完整标头之前连接已关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!