【问题标题】:Dart BrowserClient POST not including my cookiesDart BrowserClient POST 不包括我的 cookie
【发布时间】:2015-06-08 05:59:03
【问题描述】:

我正在跨域执行 BrowserClient POST,但没有看到包含我的 cookie。

这是我得到的回复:

当我发送另一个 POST 请求时,我没有看到包含 cookie:

直接进入测试页面,我可以看到包含的 cookie:

我用来做 POST 的 Dart 代码:

var client = new BrowserClient();

client.post(url, body: request, headers:{"Content-Type" : "application/json", "Access-Control-Allow-Credentials":"true"}).then((res) {
      if (res.statusCode == 200) {
        var response = JSON.decode(res.body);

        callback(response);
      } else {
        print(res.body);
        print(res.reasonPhrase);
      }
    }).whenComplete(() {
      client.close();
    });

不确定我包含的 Access-Control-Allow-Credentials 标头,不管有没有,都没有任何变化。

我是在服务器端缺少需要在响应中设置的标头,还是 Dartium 阻止了跨域 cookie?

有关Information Security 的更多详细信息以及通过服务器设置 cookie 背后的原因。

更新:已记录增强请求:https://code.google.com/p/dart/issues/detail?id=23088

更新:已实现增强功能,现在应该可以根据以下内容执行var client = new BrowserClient()..withCredentials=true; https://github.com/dart-lang/http/commit/9d76e5e3c08e526b12d545517860c092e089a313

【问题讨论】:

  • 我猜你需要在你的 post 请求上设置withCredentials=true,但我还没有找到如何处理来自 http 包的请求(比如在 stackoverflow.com/questions/21770445stackoverflow.com/questions/16939328 )。
  • 我可以看到在 angular.dart 中对此做了特殊规定,但在 BrowserClient github.com/dsalsbury/angular.dart/commit/… 中没有
  • 如果 angular.dart 正在执行此操作,那么它肯定是在底层调用标准 dart 库,还是 angular.dart 有自己的类似 BrowserClient 的库?
  • 不,在浏览器中,除了使用 dart:html 中的 HttpRequest API 之外,您不能执行 HTTP 请求。 Angular 和 browserClient 只是转发它。 http 包的创建是为了在客户端和服务器之间有一个统一的 API。在服务器上它转发到 dart:io,在浏览器上转发到 dart:html。
  • 我已经记录了一个增强请求:code.google.com/p/dart/issues/detail?id=23088 同时我会降低一点,直接使用 HttpRequest。

标签: cookies dart cors dartium


【解决方案1】:

对于发送到 CORS 请求的 cookie,您需要设置 withCredentials = truehttp 包中的浏览器客户端不支持此参数。您可以改用来自dart:htmlHttpRequest。 示例见How to use dart-protobuf

【讨论】:

  • 已经对 http 包进行了修复的拉取请求。希望尽快发布。
  • 我可以在 Github 上看到修复,发布修复的通常时间跨度是多少?如果是一周左右,我会等一会儿,否则我会按照 dart-protobuf 的例子,这样我至少可以完成我的身份验证层。
  • 根据 pub.dartlang.org 上的更改日志,两天前发布了包含此更改的版本。
  • 你可以只更新包而不是整个 Dart 安装。只需在命令行上从包目录中或从 DartEditor 中 pubspec.yaml 文件的上下文菜单中运行 pub upgrade
猜你喜欢
  • 2017-02-24
  • 2019-08-22
  • 2017-01-13
  • 2019-06-27
  • 2019-08-26
  • 2021-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多