我想在正确处理cookie的同时向远程服务器发出http请求(例如,存储服务器发送的cookie,并在我发出后续请求时发送这些cookie).保留任何和所有cookie都很好
对于我正在使用的http请求
static Future
Richard Heap.. 18
这是一个如何获取会话cookie并在后续请求中返回它的示例.您可以轻松地调整它以返回多个cookie.做一个Session
类和路由所有的GET
S和POST
通过它秒.
class Session { Mapheaders = {}; Future
Jossef Harus.. 5
我已经发布了一个名为请求的小型flutter库,以帮助支持cookie的http请求。
dependencies: requests: ^3.0.1
用法:
import 'package:requests/requests.dart'; // ... // this will persist cookies var r1 = await Requests.post("https://example.com/api/v1/login", json: {"username":"...", "password":"..."} ); r1.raiseForStatus(); // this will re-use the persisted cookies var r2 = await Requests.get("https://example.com/api/v1/stuff"); r2.raiseForStatus(); print(r2.json()['id'])
了解有关请求的更多信息
这是一个如何获取会话cookie并在后续请求中返回它的示例.您可以轻松地调整它以返回多个cookie.做一个Session
类和路由所有的GET
S和POST
通过它秒.
class Session { Mapheaders = {}; 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); } } }
我已经发布了一个名为请求的小型flutter库,以帮助支持cookie的http请求。
dependencies: requests: ^3.0.1
用法:
import 'package:requests/requests.dart'; // ... // this will persist cookies var r1 = await Requests.post("https://example.com/api/v1/login", json: {"username":"...", "password":"..."} ); r1.raiseForStatus(); // this will re-use the persisted cookies var r2 = await Requests.get("https://example.com/api/v1/stuff"); r2.raiseForStatus(); print(r2.json()['id'])
了解有关请求的更多信息