Flutter / Dart http获取请求响应不完整

时间:2019-07-11 18:18:45

标签: flutter dart

我正在尝试执行以下get请求:

  _makePlanGetRequest(int fileId) async {
    String url = 'http://192.168.1.17:8080/api/plan/download_plan/' + fileId.toString();

    print('URL: ' + url);

    HttpClient client = new HttpClient();

    var request = await client.getUrl(Uri.parse(url));
    var response = await request.close();

    await for (var contents in response.transform(Utf8Decoder())) {
      print(contents);
    }
  }

但是,当我使用此功能时,响应不完整。我在浏览器中检查了网址,响应完全正常。如何使代码正常工作?我将不胜感激。

非常感谢

2 个答案:

答案 0 :(得分:0)

我发现由于控制台的限制,我限制了控制台输出的内容。但是,当我检查响应正文的大小时,它恰好是我期望的大小。

答案 1 :(得分:0)

有一个dart程序包,为http请求提供了一些帮助程序类。

Github:https://github.com/Ephenodrom/Dart-Basic-Utils 安装:

dependencies:
  basic_utils: ^1.3.0

用法

Map<String, String> headers = {
  "Some": "Header"
};
Map<String, String> queryParameters = {
  "Some": "Parameter"
};

String url = 'http://192.168.1.17:8080/api/plan/download_plan/' + fileId.toString();

// If the api returns json
Map<String, dynamic> dataAsJson = await HttpUtils.getForJson(url,
      headers: headers, queryParameters: queryParameters);
// if the api returns plain strings
String dataAsString = await HttpUtils.getForString(url,
      headers: headers, queryParameters: queryParameters);
// if the api returns something else like XML, EPP, KV, YAML 
Response fullResponse = await HttpUtils.getForFullResponse(url,
      headers: headers, queryParameters: queryParameters);

其他信息:

这些都是HttpUtils类中的所有方法。

Future<Map<Response> getForFullResponse(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> getForJson(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<String> getForString(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
Future<Map<Response> postForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> postForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> postForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Response> putForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> putForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> putForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Response deleteForFullResponse(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Future<Map<String, dynamic>> deleteForJson(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Future<String> deleteForString(String url,{Map<String, String> queryParameters,Map<String, String> headers});
Map<String, dynamic> getQueryParameterFromUrl(String url);
String addQueryParameterToUrl(String url, Map<String, dynamic> queryParameters);