博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cookie ====================
阅读量:4570 次
发布时间:2019-06-08

本文共 4229 字,大约阅读时间需要 14 分钟。

https://stackoverflow.com/questions/52241089/how-do-i-make-an-http-request-using-cookies-on-flutter?rq=1

 

Here's an example of how to grab a session cookie and return it on subsequent requests. You could easily adapt it to return multiple cookies. Make a Session class and route all your GETs and POSTs through it.

===

import 'package:http/http.dart' as http; import 'dart:convert';
class Session {  Map
headers = {}; Future
get(String url) async { http.Response response = await http.get(url, headers: headers); updateCookie(response); return response.body; } Future
post(String url, dynamic data) async { http.Response response = await http.post(url, body: data, headers: headers); updateCookie(response); return response.body; } void updateCookie(http.Response response) { String rawCookie = response.headers['set-cookie']; if (rawCookie != null) { int index = rawCookie.indexOf(';'); headers['cookie'] = (index == -1) ? rawCookie : rawCookie.substring(0, index); } }}

  

 
class Session {  Map
headers = {}; Future
get(String url) async { http.Response response = await http.get(url, headers: headers); updateCookie(response); return json.decode(response.body); } Future post(String url, dynamic data) async { http.Response response = await http.post(url, body: data, headers: headers); updateCookie(response); return json.decode(response.body); } void updateCookie(http.Response response) { String rawCookie = response.headers['set-cookie']; if (rawCookie != null) { int index = rawCookie.indexOf(';'); headers['cookie'] = (index == -1) ? rawCookie : rawCookie.substring(0, index); } }}

  

 

I have improved the Richard Heap's solution to be capable to process multiple 'Set-cookies' and multiple cookies.

In my case, the server returns multiples 'Set-cookies'. The http package concatenate all the set-cookies headers in one header and separate it by comma (',').

class NetworkService {  final JsonDecoder _decoder = new JsonDecoder();  final JsonEncoder _encoder = new JsonEncoder();  Map
headers = {"content-type": "text/json"}; Map
cookies = {}; void _updateCookie(http.Response response) { String allSetCookie = response.headers['set-cookie']; if (allSetCookie != null) { var setCookies = allSetCookie.split(','); for (var setCookie in setCookies) { var cookies = setCookie.split(';'); for (var cookie in cookies) { _setCookie(cookie); } } headers['cookie'] = _generateCookieHeader(); } } void _setCookie(String rawCookie) { if (rawCookie.length > 0) { var keyValue = rawCookie.split('='); if (keyValue.length == 2) { var key = keyValue[0].trim(); var value = keyValue[1]; // ignore keys that aren't cookies if (key == 'path' || key == 'expires') return; this.cookies[key] = value; } } } String _generateCookieHeader() { String cookie = ""; for (var key in cookies.keys) { if (cookie.length > 0) cookie += ";"; cookie += key + "=" + cookies[key]; } return cookie; } Future
get(String url) { return http.get(url, headers: headers).then((http.Response response) { final String res = response.body; final int statusCode = response.statusCode; _updateCookie(response); if (statusCode < 200 || statusCode > 400 || json == null) { throw new Exception("Error while fetching data"); } return _decoder.convert(res); }); } Future
post(String url, {body, encoding}) { return http .post(url, body: _encoder.convert(body), headers: headers, encoding: encoding) .then((http.Response response) { final String res = response.body; final int statusCode = response.statusCode; _updateCookie(response); if (statusCode < 200 || statusCode > 400 || json == null) { throw new Exception("Error while fetching data"); } return _decoder.convert(res); }); }}

  

 

 

转载于:https://www.cnblogs.com/pythonClub/p/10717814.html

你可能感兴趣的文章
优化 SQL SELECT 语句性能
查看>>
Spring3 MVC 类型转换
查看>>
9260与SAM-BA连接(转)
查看>>
不要忽略'\'
查看>>
require php中引用函数
查看>>
字符串操作练习:星座、凯撒密码、99乘法表、词频统计预处理
查看>>
Linux工具之Vim使用
查看>>
poj1860 bellman—ford队列优化 Currency Exchange
查看>>
【成长大小事】吃饭+挣钱=在深圳
查看>>
找茬脚本思路(修改中)
查看>>
Java创建线程的细节分析
查看>>
python语法_深浅拷贝
查看>>
使用CCleaner卸载chrome
查看>>
typeof和GetType的区别
查看>>
xtraTabbedMdiManager控件切换时控件不更新的问题
查看>>
为易信正名
查看>>
debian8.4 ibus中文输入法
查看>>
《JAVA程序设计》实训第一天——《猜猜看》游戏
查看>>
P1896 [SCOI2005]互不侵犯
查看>>
ESP定律手工脱壳步骤
查看>>