我目前正在尝试使用Dio从GET或POST请求中检索多个Set-Cookie字段。我无法使用HttpClient或flutter的http来做到这一点。
使用Dio会引发异常:
HttpException:标头set-cookie有多个值
我想知道如何解决这个问题并处理多个set-cookie
标头,然后将它们发送回cookie
标头中。
如何处理多个Set-Cookie
标头,并使用Dart / Flutter将它们作为Cookie标头发送回去?
这是MCVE
Dio dio = new Dio();
dio.get(urlLogin).then((Response resp){
print('-----Login-----');
print(resp.headers.value('set-cookie'));
});
还有StackTrace
HttpException: More than one value for header set-cookie
#0 _HttpHeaders.value (dart:_http/http_headers.dart:48:7)
#1 loginDio.<anonymous closure> (file:///home/fuguet/Prog/Dart/FPlogin/main.dart:55:24)
#2 _RootZone.runUnary (dart:async/zone.dart:1379:54)
#3 _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
#4 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
#5 Future._propagateToListeners (dart:async/future_impl.dart:671:32)
#6 Future._complete (dart:async/future_impl.dart:476:7)
#7 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
#8 _RootZone.runUnary (dart:async/zone.dart:1379:54)
#9 _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
#10 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
#11 Future._propagateToListeners (dart:async/future_impl.dart:671:32)
#12 Future._complete (dart:async/future_impl.dart:476:7)
#13 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
#14 _RootZone.runUnary (dart:async/zone.dart:1379:54)
#15 _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
#16 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:642:45)
#17 Future._propagateToListeners (dart:async/future_impl.dart:671:32)
#18 Future._completeWithValue (dart:async/future_impl.dart:486:5)
#19 Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:516:7)
#20 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#21 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
#22 _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:391:30)
#23 _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)
#24 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:165:12)
答案 0 :(得分:3)
由于可能有多个同名标题,所以不能使用value()
。而是使用[]
来检索List<String>
。
List<String> rawCookies = resp.headers['set-cookie'];