FormatException iOS上的无效HTTP标头Flutter

时间:2019-05-24 13:25:33

标签: ios flutter http-headers

此代码可以完美地在Android设备和模拟器上运行。在 iOS 模拟器上不起作用。

我尝试将String更改为小写字母,删除空格等,但无法解决。

import 'package:http/http.dart' show Client;
...
Map<String,String> headers = Map();
headers['device'] = 'appleiphonexʀ';//'Apple-iPhone-Xʀ'//'Apple_iPhone_Xʀ'//'Apple iPhone Xʀ'
...
var response = await client.get(Uri.parse(url), headers: headers);
return await processResponse(response);

引发异常:

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: FormatException: Invalid HTTP header field value: "appleiphonexʀ"
  _HttpHeaders._validateValue (dart:_http/http_headers.dart:601:9)
  _HttpHeaders._addAll (dart:_http/http_headers.dart:65:18)
  _HttpHeaders.set (dart:_http/http_headers.dart:76:5)
  IOClient.send.<anonymous closure> (package:http/src/io_client.dart:42:27)
  __CompactLinkedCustomHashMap&_HashFieldBase&MapMixin&_LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:367:8)
  IOClient.send (package:http/src/io_client.dart:41:23)
 < asynchronous suspension>
  BaseClient._sendUnstreamed (package:http/src/base_client.dart:169:38)
 < asynchronous suspension>
  BaseClient.post (package:http/src/base_client.dart:54:7)
  NetworkProvider.post (package:PROJECTXX/src/models/resources/network_provider.dart:24:22)
<asynchronous suspension>

已解决:see answer

2 个答案:

答案 0 :(得分:1)

标头中的最后一个字符是Unicode代码点hex 0280LATIN LETTER SMALL CAPITAL R。根据RFC 2616,HTTP标头必须仅包含ISO-8859-1中的字符,除非它们以MIME格式编码(看起来像=?UTF-8?Q?=E2=9C=B0?=)。可以改用小写或大写的ASCII R吗?

答案 1 :(得分:0)

跟随Richard Heap comment,问题是最后一个字符,该数据来自lib device_info:^ 0.4.0 + 1

final DeviceInfoPlugin deviceInfoPlugin = new DeviceInfoPlugin();
PackageInfo packageInfo = await PackageInfo.fromPlatform();
...
var device = await deviceInfoPlugin.iosInfo.name;//Apple iPhone Xʀ
...
//the solution: remove non ascii chars and substitute for underline        
headers['device'] = device.replaceAll(new RegExp('[^\u0001-\u007F]'),'_');